I want to stop this counter when it reaches to 0, can anyone help me?
function countdown() {
var i = document.getElementById('counter');
if (parseInt(i.innerHTML)<=0) {
}
i.innerHTML = parseInt(i.innerHTML)-1;
}
setInterval(function(){ countdown(); },1000);
You use the clearInterval function:
var id;
function countdown() {
var i = document.getElementById('counter');
if (parseInt(i.innerHTML)<=1) {
clearInterval(id);
}
i.innerHTML = parseInt(i.innerHTML)-1;
}
id = setInterval(countdown,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