I need to make a simple countdown timer from 5 to zero, with the buttons to START and STOP the counter. The only thing that I don\'t know is that why won\'t my counter stop.
myTimer
is only in scope within the function. One solution is to make it global.
var myTimer;
function clock() {
myTimer = setInterval(myClock, 1000);
var c = 5;
function myClock() {
document.getElementById("demo").innerHTML = --c;
if (c == 0) {
clearInterval(myTimer);
alert("Reached zero");
}
}
}
5