I have been working on a count down for most of today, and now I am stuck on looping. Without the for loop everything works fine the count down hits 0 and reloads, but with the for loop it doesn't count down properly and skips numbers. What I would like to accomplished here is to have the timer count down completely and after 3 complete count downs it will stop completely. What am I doing wrong here?
var number = 25;
var i;
function countdown() {
$('#display').html("Redirecting in " + number + " second(s).");
for (i = 0; i < 3; ++i) {
number--;
if (number < 0) {
window.location.reload();
number = 0;
}
}
setTimeout(countdown, 1000);
}
$(document).ready(function() {
countdown();
});
numberfour times in the loop--of course it skips numbers. You also reload, which will start the whole process over again--unless you store the "total" count somewhere persistent, like in a cookie or something, every time you reload your JS starts all over.numberin yourforloop 4 times. Change it tofor (i = 0; i < 1; ++i) {and it will work. But what's the point of the loop in that case?number, andround), decreasenumberevery time, and add special handling whennumberreaches 0.