1

I have a Javascript Array that holds the contents of a page. In order to draw all the objects in the right places when the page loads I loop over the array and pull out the elements. This worked very well until I allowed the objects to have children within them.

The current array structure is 0-> {elements=[] frame={I keep the frame attributes here with key value pairs}}, 1-> {elements=[] frame={}}

However, I just started adding sub-elements to the elements array in each object. So now I have to loop through/draw each element, check to see if there are any children and if so then I have to draw them too.

The problem I'm having is that after I loop through the first 0 object and it's children the for loop stops running. Is it because I'm calling the same function multiple times? I've done that before so I don't think that's what is happening.

this.run_loop = function (spawn, dom) {
    console.log(spawn)
    alert(spawn.length)
    for (i = 0; i < spawn.length; i++) {
        console.log(spawn[i])
        //alert("i one")    
        var newdom = dom + "_" + i;
        this.synthesize_elements(spawn[i], dom, newdom)
        if (spawn[i].hasOwnProperty('elements')) {
            //alert("FOUND")
            var newarray = spawn[i]['elements'];
            if (newarray.length > 0) {
                this.run_loop(newarray, newdom)
            }
        }
    }
}
4
  • If you open the console does it show any runtime errors? Commented Dec 5, 2013 at 19:06
  • No. It just doesn't run. If I comment out this.run_loop(newarray, newdom) it runs through the top two arrays perfectly. It's only when I loop it through the child arrays that I have a problem Commented Dec 5, 2013 at 19:10
  • 3
    Could you create a minimal complete example that demonstrates your problem? I can't test your code because you are referencing functions that aren't defined in your example. Commented Dec 5, 2013 at 19:16
  • Agree with @PeterOlson, it's probably a bug in other code not shown here; for example the synthesize_elements call probably doesn't do what you think it does. Commented Dec 5, 2013 at 19:31

1 Answer 1

3

This is a little old but I encountered a similar issue and found my way here, but I figured it out and thought I'd post the solution if anyone else came across this. The issue in my case (and it looks like in your case as well though I can't be sure) was in the declaration of the for loop:

for (i = 0; i < spawn.length; i++)

You're not declaring that i is a new var. so if anything inside the

this.run_loop(newarray, newdom)

function also manipulates a counter variable called i that isn't declared a new var, it will also change the one in your outer scope, and kick you out of the loop if it goes over the length of spawn

just always declare:

for (var i; i< spawn.length; i++)

in your loops or make sure your counters are unique.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.