views:

230

answers:

2

I have an array that returns multidimensional data from an mysql database, when this is collected the createNews function creates the user interface. The problem I am having is the loop is iterating quicker than the UI is being created, is there a way to use event listeners with loops so it only continues after my function has completed its work?

var t:Array = responds.serverInfo.initialData;  
for (var i:uint = 0; i < t.length; i++) {
    var date = t[i][1];
    var newstitle = t[i][2];
    var story= t[i][3];
    var image = t[i][4];

    createNews(date, newstitle, story, image);    
}
A: 

you can have a custom callback from when you're done creating the ui element, which would then process the next item in the array.

var t:Array = responds.serverInfo.initialData;
var numProcessed:int = 0;

private function processNext() : void {
    if (numProcessed == t.length) return;
    var date = t[numProcessed][1];
    var newstitle = t[numProcessed][2];
    var story= t[numProcessed][3];
    var image = t[numProcessed][4];

    numProcessed++;

    createNews(date, newstitle, story, image);  
}

depending on how your createNews function works and how everything is structured, your process of calling processNext when the ui is done will differ. I'm guessing that createNews would create a new class or something, in that case you would attach an event listener to that new class, listening for a COMPLETE event which will be dispatched when creation is complete, and setting processNext as the event handler.

jonathanasdf
both seem to work, I didn't know flash was single threaded. Although createnews() does call about another 3 functions so maybe that is it?
Ross
A: 

Doesn't your createNews function block the loop until it returns? Actionscript is single-threaded so I'm not sure how the loop could continue when the createNews function hasnt finished yet.

teehoo
say the loaded component transitions (tweens) in, or loads something using loader or urlloader, or using a timerEvent, or setTimeout. These would all be async
jonathanasdf