I have a 5 minutes countdown. This function launch automatically when page load. I want to reload data of page and resetting this countdown. The code looks like this.
HTML
<a onClick="timer()" title="Pincha para actualizar" class="pull-right" id="time"></a>
JS
function timer(){
var timer = 60 * 5, minutes, seconds;
setInterval(function () {
minutes = parseInt(timer / 60, 10)
seconds = parseInt(timer % 60, 10);
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
$('#time').text("Tiempo restante para actualización de datos " + minutes + ":" + seconds);
if (--timer < 0) {
timer = duration;
}
}, 1000);
}
The problem: When i click the link the timer goes crazy, doesn´t reset properly.
Anybody knows what´s the rigth way to reset countdown?