Reputation: 79
<script type="text/javascript">
function countdown(minutes) {
var seconds = 60;
var mins = minutes
function tick() {
var counter = document.getElementById("timer");
var current_minutes = mins-1
seconds--;
counter.innerHTML = current_minutes.toString() + ":" + (seconds < 10 ? "0" : "") + String(seconds);
if( seconds > 0 ) {
setTimeout(tick, 1000);
} else {
if(mins > 1){
// countdown(mins-1); never reach “00″ issue solved:Contributed by Victor Streithors
setTimeout(function () { countdown(mins - 1); }, 1000);
alert("time has ended");
//here i have to auto submit the form when time ends
}
}
}
tick();
}
countdown(10);
</script>
how to show alert when the time has ended and the timer should stop running i have tried the following code the but timer stops as (00.00) and it does not show any alert messsage and i want to auto submit a form if ends, can anyone help me please
Upvotes: 2
Views: 95
Reputation: 36609
You will need another
else
condition.
Try this:
function countdown(minutes) {
var seconds = 60;
var mins = minutes
function tick() {
var counter = document.getElementById("timer");
var current_minutes = mins - 1
seconds--;
counter.innerHTML = current_minutes.toString() + ":" + (seconds < 10 ? "0" : "") + String(seconds);
if (seconds > 0) {
setTimeout(tick, 1000);
} else {
if (mins > 1) {
setTimeout(function() {
countdown(mins - 1);
}, 1000);
} else {
alert("time has ended");
}
}
}
tick();
}
countdown(2);;
<div id="timer"></div>
Upvotes: 2