A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: XML For Each loop with timer?

  1. #1
    Member
    Join Date
    Nov 2007
    Posts
    36

    XML For Each loop with timer?

    I've loaded an xml file and I want to iterate through it with a for each loop. How do I pause the loop after each iteration for a bit before continuing?

    One idea I've been thinking is set up a timer and in the loop call a function that sits for a while looking at it's own navel for x seconds then returns back to the loop to let it continue. How do I get my code to look at it's own navel? (if there's no easier way to do it)

    [edit]
    what I have atm is
    Code:
    function wait():void{
    	myTimer.start();
    	while (myTimer.running){};
    }
    This just makes my code sit there. Any ideas?
    Last edited by cintaria; 01-19-2009 at 06:17 PM.

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    There is no way to get flash to pause like you want. What you can do is set up a Timer and an event listener.

    Rough draft code:

    Code:
    var nodes:XMLList; //the nodes you're interested in.  populated outside this snippet.
    var nodeArray:Array = new Array();
    
    for each (var node:XML in nodes){
      nodeArray.push(node);
    }
    
    var xmlTimer:Timer = new Timer(1000, nodeArray.length); //1 second
    xmlTimer.addEventListener(TimerEvent.TIMER, processANode);
    xmlTimer.start();
    
    function processANode(e:TimerEvent):void{
      processXML(nodeArray.shift());
    }
    Where processXML is your node processing function.

  3. #3
    Member
    Join Date
    Nov 2007
    Posts
    36
    Thanks for the reply. Your example code got me thinking and I came up with

    Code:
    var myTimer:Timer = new Timer(10000); //10 seconds
    counter =0;
    myTimer.addEventListener(TimerEvent.TIMER, extract);
    
    function extract(event:TimerEvent):void {
    	//do something interesting with xml
    	text1.text = xml.INFO.CHILD[counter];
    	//etc...
    
    	if (counter == xml.length()){
    		counter=0;
    	} else {
    		counter++
    	}
    	return;
    }
    
    //Is called when XML upload is complete
    function completeListener(e:Event):void{
    	xml = new XML(urlLoader.data);
    	myTimer.start();
    }
    This will also loop the interesting thing with the xml A limit on the number of loops could be implemented by putting a repeat int in the Timer constructor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center