What is wrong with this code I seem to be geting an error that timer is not defined
var counter = setInterval("timer()",1000);
function timer(){
count = count-1;
if(count <=0){
clearInterval(counter);
return;
}
document.getElementById("timer").innerHTML = count + " sec";
}
setInterval
.Your function is a local variable, which doesn't exist when setTimeout
eval's the string in the global scope.
Instead, pass the function itself to setInterval
:
var counter = setInterval(timer, 1000);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With