Thursday, 5 September 2013

How do i create a countdown timer?

How do i create a countdown timer?

I am a beginner programmer, just started JavaScript. I found a countdown
timer that looks like this,
var count=60 ;
var Time_counter=setInterval(timer, 1000) ;
function timer(){
count=count-1 ;
if(count <=1){
clearInterval(Time_counter);
alert("Game Over") ;
return ;}
document.getElementById("timer").innerHTML=count;
}
Now when ever i run this program, this timer runs instantly without any
conditions, it happens even if i put conditions .It will run by itself
even if i keep it in an onclick attribute in the input tag like this :
<input id="Timer_button" type="button" value="Time starter"
onclick="timer()">
but still the same problem.Even if i don't call the function, it just
runs. Can anybody find out the problem in the code, is there something
wrong going on here? Answers would be really really appreciated. Please
advices required .

No comments:

Post a Comment