Javascript or jquery countdown counter?

I am trying to create a simple counter that counts back from 3 to 0 or from 5 to 0 or whatever.

This is a timer for questions, so each number should be visible to the user.

I tried:

for (i=3;i>=0;i--){ $(".timerInner").delay(500).text( i ); } 

But no luck.

0
source share
5 answers

Try it,

 var i =5; var timer = setInterval( calltimer, 500); function calltimer(){ $(".timerInner").append( i ); if(i == 0){ clearInterval(timer); } i--; } 
+2
source

Use setInterval / clearInterval instead

 var i = 5; var t = setInterval(function() { i === 0 && clearInterval(t); $(".timerInner").text(i); i--; }, 1000);​ 
+3
source

I recommend using the setTimeout JavaScript setTimeout :

 function countdown(remainingTime) { $('.timerInner').text(remainingTime); if (remainingTime > 0) setTimeout(function() { countdown(remainingTime - 100); }, 100); } ​countdown(1000);​ 

Example script for playing with: http://jsfiddle.net/MSa8h/1/

+1
source

You need to create a loop bass in the setTimeout function, here's a sketch

 var i = 3; var calc = function(){ i--; if(i==0){ //start new iteration timeout(); }else{ //end loop return; } }; var timeout = function(){ setTimeout(calc,1000); }; 
0
source
 var i =5; var timer = setInterval( calltimer, 500); function calltimer(){ $(".timerInner").text( i ); if(i == 0){ clearInterval(timer); } i--; } 

thanks for the help

0
source

Source: https://habr.com/ru/post/1412392/


All Articles