Bryan Parmenter
Bryan Parmenter

Reputation: 60

Javascript Infinite Loop does not loop

I am trying to make a Javascript that loops infinitly.

function scroll(num) {
    $("." + num + "").fadeOut('slow');
    var choose = num + 1;
    $("." + choose + "").fadeIn('slow');
    setTimeout(function() {
        scroll(choose);
    }, 1000);
}

setTimeout(function() {
    scroll('1');
}, 1000);​

It does the function the first time but does not continue.

Please help!

Upvotes: 0

Views: 96

Answers (3)

Vivek Panday
Vivek Panday

Reputation: 1436

Here Problem is with your Concatenating,using + to Concatenating is ambiguous in your code . In your code var choose = num + 1; will return 11 as string not 2 as per your expectation.

Upvotes: 0

Tom
Tom

Reputation: 8180

you are adding 1 to the string "1" resulting in "11". is this what you meant to do?

Upvotes: 0

sczizzo
sczizzo

Reputation: 3206

You're using one as both a string and as an integer: Calling scroll('1') means that num + 1 will return "11", not 2 as you expect. Try this instead:

function scroll(num) {
    $("." + num.toString()).fadeOut('slow');
    var choose = num + 1;
    $("." + choose.toString()).fadeIn('slow');
    setTimeout(function() {
        scroll(choose);
    }, 1000);
}

setTimeout(function() {
    scroll(1);
}, 1000);​

Upvotes: 3

Related Questions