tags:

views:

127

answers:

3

for some sick reason, my check productIDs[addIndex] = allProductIDs[lastProductFoundIndex + i]; causes my app to spin into an infinite loop:

numberOfImagesToDisplay is set to 4

if (state == "next")
{
    for(var a = 0; a < numberOfImagesToDisplay; a++) {
        alert("a=" + a + ", numImages=" + numberOfImagesToDisplay)

        if (a > 0) { addIndex = productIDs.length + 1; }
        alert("I'm in GetNextProductIDs() 1");
        //var lastProductFoundIndex = $.inArray(lastProductID, allProductIDs);
        //alert("I'm in GetNextProductIDs() 2");

        if (lastProductIndex >= 0) {
            alert("I'm in GetNextProductIDs() 3");
            //productIDs[addIndex] = allProductIDs[lastProductFoundIndex + i];
        }
    }
}

If I take out that line, it moves on.

Update: Resolved. lastProductIndex was not defined. So what was happening is that it would get there and the loop would end but it's weird because a callback was being called again when it should have ended. So that callback method kept calling this method and this method would end at that spot, the callback method would again be called, and so you had an endless loop.

+1  A: 

That's very strange. All I can think is that you have an onpropertychange event firing that also modifies i. Major longshot, I know.

What if you add the var keyword to your for loop? That would turn it into a local variable instead of a global variable so no other function could inadvertantly trash your loop index.

for (var i = 0; i < numberOfImagesToDisplay; i++)

Note: You should have the var there whether or not that's the problem.

Update: What does alert("i="+i+", numImages="+numberOfImagesToDisplay) display each iteration through your loop? Do those variables have the expected values?

Are you sure this loop is stuck? Maybe it's another loop. Could it be that you're re-entering this loop repeatedly thereby getting repeated alerts? I just don't see how that line could cause this loop to become an infinite loop.

John Kugelman
var made no difference. Again if I remove this line, all is fine and it exits my function just fine: productIDs[addIndex] = allProductIDs[lastProductFoundIndex + i];
CoffeeAddict
weird, I get 0 and 4 every time. wtf
CoffeeAddict
+1  A: 

I don't see you incrementing variable a in there anywhere, but you're incrementing a variable i in your loop. a will therefore always be 0 -

for(var a = 0; a < numberOfImagesToDisplay; i++)
Russ Cam
I had updated the original post...supposed to be an a, and a is what I have in my code. Sorry.
CoffeeAddict
+1  A: 

It's not related to your suspected problem line, but, at the line

for(var a = 0; a < numberOfImagesToDisplay; i++)

your setting a = 0 and the loop will run while a < numberOfImagesToDisplay. I dont see anywhere where you are incrementing or changing a to exit the for loop.

Graham Miller