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!
Solution 1:
with setTimeout
:
var n = 100;
setTimeout(countDown,1000);
functioncountDown(){
n--;
if(n > 0){
setTimeout(countDown,1000);
}
console.log(n);
}
or using setInterval
:
var n = 100;
var tm = setInterval(countDown,1000);
functioncountDown(){
n--;
if(n == 0){
clearInterval(tm);
}
console.log(n);
}
Solution 2:
<script>var timer = setInterval("mytimer()",1000);
seconds = 0;
functionmytimer()
{
document.getElementById("div_timer").innerHTML = seconds; // this is the same as $("div_timer").html(timer) in jquery.
seconds++;
}
</script><body><divid="div_timer"></div></body>
Post a Comment for "Jquery Show Settimeout Timer"