# Problem with for loops

**URL:** <https://forum.kirupa.com/t/problem-with-for-loops/264343>\
**Category:** Uncategorized\
**Created:** [June 25, 2008, 5:16am UTC](https://forum.kirupa.com/t/problem-with-for-loops/264343 "2008-06-25T05:16:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![theSchtickler](https://avatars.discourse-cdn.com/v4/letter/t/ba9def/32.png) [@theSchtickler](https://forum.kirupa.com/u/theSchtickler)\
**Post date:** [June 25, 2008, 5:16am UTC](https://forum.kirupa.com/t/problem-with-for-loops/264343/1 "2008-06-25T05:16:44Z")

</div>

Hi there,

I’m having some issues with this loop:

[AS]for(var i=0; i\<10; i++){  
\_root[‘myXML’ + i] = new XML();  
\_root[‘myXML’ + i].ignoreWhite = true;  
\_root[‘myXML’ + i].onLoad = function(ok) {  
if (ok) {  
allContent\* = this.firstChild.firstChild.nodeValue;  
amLoaded\* = true;   
}  
}  
\_root[‘myXML’ + i].load(‘content/’ + names\* + ‘/text.xml’ );  
}[/AS]

So, I know there is an issue with this kind of thing. I use a similar script to allocate actions to MCs, but with an MC, I can store a variable inside it, so it knows who it is. With this script above, because they all come in at the same time, it only shows one lot of content.

Any suggestions on how to rewrite it? Please bare in mind, that I am actually trying to load them all at once, and not sequentially (if that makes sense)

Thanks.

---

<div class="post-metadata">

**Author:** ![randomagain](https://avatars.discourse-cdn.com/v4/letter/r/35a633/32.png) [@randomagain](https://forum.kirupa.com/u/randomagain)\
**Post date:** [June 25, 2008, 11:43am UTC](https://forum.kirupa.com/t/problem-with-for-loops/264343/2 "2008-06-25T11:43:22Z")

</div>

maybe make sense to load one xml in, then start the loop with the next 😛

there not loading in at once if you have a for loop, they are still techniquely running a millisecond behind each other

I think helps to know what app it is for

---

<div class="post-metadata">

**Author:** ![RubenFlash](https://avatars.discourse-cdn.com/v4/letter/r/839c29/32.png) [@RubenFlash](https://forum.kirupa.com/u/RubenFlash)\
**Post date:** [June 25, 2008, 2:47pm UTC](https://forum.kirupa.com/t/problem-with-for-loops/264343/3 "2008-06-25T14:47:43Z")

</div>

Create a recursive function:

```auto

var howManyXMLs:Number = 10;
var count:Number = 0;

function loadXML(xml) {
	xml = new XML();
	xml.ignoreWhite = true;
	xml.load('content/'+names[count]+'/text.xml');
	xml.onLoad = function(ok) {
		if (ok) {
			allContent[count] = this.firstChild.firstChild.nodeValue;
			amLoaded[count] = true;
			count++;
			if(count<10){
				loadXML(_root['myXML'+count]);
			}
		}
	};
}

loadXML(_root['myXML'+count]);

```

---

<div class="post-metadata">

**Author:** ![theSchtickler](https://avatars.discourse-cdn.com/v4/letter/t/ba9def/32.png) [@theSchtickler](https://forum.kirupa.com/u/theSchtickler)\
**Post date:** [June 25, 2008, 11:38pm UTC](https://forum.kirupa.com/t/problem-with-for-loops/264343/4 "2008-06-25T23:38:01Z")

</div>

Thanks RubenFlash, that worked a treat! Can you tell me why that works and a for loop doesn’t? For me that seems to be doing the same thing?
