Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery show setTimeout timer

I'm trying to build a simple countdown application. Is it possible to show the timer value on setTimeout, or would I have to use a for loop?

Thanks!

like image 399
sophistry Avatar asked Jul 27 '12 17:07

sophistry


1 Answers

with setTimeout :

var n = 100;
setTimeout(countDown,1000);

function countDown(){
   n--;
   if(n > 0){
      setTimeout(countDown,1000);
   }
   console.log(n);
}

or using setInterval :

var n = 100;
var tm = setInterval(countDown,1000);

function countDown(){
   n--;
   if(n == 0){
      clearInterval(tm);
   }
   console.log(n);
}
like image 198
mgraph Avatar answered Sep 27 '22 16:09

mgraph