Created
November 21, 2015 23:45
-
-
Save brenopolanski/f326b53954769664f21f to your computer and use it in GitHub Desktop.
Start, Stop and Reset Javascript SetInterval
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="btns"> | |
<a href="" id="set" class="btn">Set/Reset</a> | |
<a href="" id="stop" class="btn">Stop</a> | |
</div> | |
<div id="container"></div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var timer = 0; | |
function setResetInterval(bool){ | |
var el = $("#container"); | |
if(bool){ | |
timer = setInterval(function(){ | |
el.css("background", "red"); | |
setTimeout(function(){ | |
el.css("background", "blue"); | |
},500); | |
},1000); | |
}else{ | |
clearInterval(timer); | |
} | |
} | |
$("#set").click(function(e){ | |
e.preventDefault(); | |
setResetInterval(true); | |
}); | |
$("#stop").click(function(e){ | |
e.preventDefault(); | |
setResetInterval(false); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#container{ | |
background: blue; | |
width: 50px; | |
height: 50px; | |
} | |
.btn{ | |
text-decoration:none; | |
background: grey; | |
color: #fff; | |
padding: 5px; | |
margin: 5px; | |
margin-bottom: 10px; | |
} | |
.btns{ | |
height: 50px; | |
display:block; | |
padding: 3px; | |
} |
@tepetar mentioning me is what you did wrong, either way, it should work, here is a working sandbox of this same code: https://codesandbox.io/embed/sharp-hodgkin-jinir?fontsize=14
EDIT: Don't download the zip file, I see your problem now. The html here is just an example, it does not import the script or the styles...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Code in ZIP file is not working ! Is it true or I @am doing something wrong ? Except two text marks "Set/Reset" and "Stop" I can not get anything else - no buttons, no container... What version of jQuery need I to use ?