bammab
bammab

Reputation: 2563

how to loop using jquery

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();
});

Upvotes: 0

Views: 259

Answers (1)

Jeffrey
Jeffrey

Reputation: 2005

I take it you want to count down from 25 for 3 times, and then reload the page. Am I right?

var number = 25;
var i = 0;

function countdown() {

    $('#display').html("Redirecting in " + number + " second(s).");

    number--;

    if (number == 0) {

        number = 25;
        i++;

    }

    if (i == 3) {

        window.location.reload();

    }

    setTimeout(countdown, 1000);

}

$(document).ready(function() {
    countdown();
});

Upvotes: 1

Related Questions