- How to stop my javascript countdown?
- var settimmer = 0;
- $(function(){
- window.setInterval(function() {
- var timeCounter = $("b[id=show-time]").html();
- var updateTime = eval(timeCounter)- eval(1);
- $("b[id=show-time]").html(updateTime);
- }, 1000);
- });
- <b id="show-time">20</b>
- $(function(){
- var timer = setInterval(function() {
- var timeCounter = parseInt($("b[id=show-time]").text());
- $("b[id=show-time]").text(--timeCounter); // remove one
- if(!timeCounter) clearInterval(timer);
- }, 1000);
- });
- // Activate timer
- var iv = window.setInterval(...);
- // Deactive timer
- window.clearInterval(iv);
- $(function() {
- // Read the start value once and store it in a variable
- var timeCounter = parseInt( $("b[id=show-time]").text() );
- // Active the counter
- var iv = window.setInterval(function() {
- // Decrement by one and write back into the document
- $("b[id=show-time]").text(--timeCounter);
- // Check if counter == 0 -> stop counting
- if (0 == timeCounter) {
- window.clearInterval(iv);
- // ...do whatever else needs to be done when counter == 0 ..
- }
- }, 1000);
- });
- var i = 0,
- pid = setInterval(function() {
- if (++i > 10)
- clearInterval(pid);
- }, 1000);
- $(function() {
- var el = document.getElementById('show-time'),
- pid = setInterval(function() {
- // (s - i) coerces s to Number
- var t = el.innerHTML - 1;
- el.innerHTML = t;
- if (t < 1)
- clearInterval(pid);
- }, 1000);
- });
- <script type="text/javascript">
- $(function(){
- var settimmer = 0,
- timeCounter = $("#show-time").html(),
- updateTime = timeCounter;
- (function countDown() {
- timeCounter = $("#show-time").html();
- updateTime = parseInt(timeCounter)-1;
- $("#show-time").html(updateTime);
- if ( updateTime ) {
- setTimeout(countDown, 1000);
- }
- })();
- });
- </script>
- $(function(){
- var elem=$('strong[id="show-time"]'),settimmer=0,updateTime,t;
- t=window.setInterval(function() {
- updateTime=parseFloat(elem.html(),10)-1;
- if(updateTime==0) {
- window.clearInterval(t);
- elem.html('Done!');
- } else {
- elem.html(updateTime);
- }
- },1000);
- });
- <strong id="show-time">20</strong>