as3 loops and event listeners
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);
}
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.
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.
精彩评论