How can I display the timer value, though?
- sophistry
@Rocketeer: Are you asking how to display the current time? Please be more specific?
- SLaks
You'd set the content of an element to current countdown value.
- Felix Kling
Yeah, current time remaining on the timer. I would have to use some variant of increment function?
- sophistry
2 Answers
22
votes
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);
}
-1
votes
<script>
var timer = setInterval("mytimer()",1000);
seconds = 0;
function mytimer()
{
document.getElementById("div_timer").innerHTML = seconds; // this is the same as $("div_timer").html(timer) in jquery.
seconds++;
}
</script>
<body>
<div id="div_timer"></div>
</body>
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.OkRead more
setTimeoutis the way to go. Example: stackoverflow.com/questions/2835087/countdown-timer - Felix Kling