我只是想在 Vesper 的回答中添加一些内容。使用 EnterFrame 毫无意义,因为该事件正在执行相同的工作。只需等待事件被触发,然后再执行其他任何操作。
var Data:String;
var weAreReady:Boolean=false;
getData("file:///C:/..../try.htm");
function getData(url:String):void
{
var phpFileRequest:URLRequest = new URLRequest(url);
var phpLoader:URLLoader = new URLLoader();
phpLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
phpLoader.addEventListener(Event.COMPLETE, showResult);
phpLoader.load(phpFileRequest);
}
function showResult(event:Event):void
{
Data= "" + event.target.data;
trace(Data);
goFurther(); // this will be called when the data will be finally ready
}
如果您想同时调用不同的请求并希望等待所有事件被触发,请再次避免将 EnterFrame 或 Timer 添加到混合中。首先,我会避免多个同时请求,而是将它们排队,这似乎是控制整个事情的更好方法。但如果你还是想这样做,我会这样写:
var Data:String;
var _total:int; //number of events
var _done:int; //number of events triggered
var weAreReady:Boolean=false;
_total = 4;
_done = 0;
getData("file:///C:/..../try.htm");
getData("file:///C:/..../try2.htm");
getData("file:///C:/..../try3.htm");
getData("file:///C:/..../try4.htm");
function getData(url:String):void
{
var phpFileRequest:URLRequest = new URLRequest(url);
var phpLoader:URLLoader = new URLLoader();
phpLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
phpLoader.addEventListener(Event.COMPLETE, showResult);
phpLoader.load(phpFileRequest);
}
function showResult(event:Event):void
{
Data= "" + event.target.data;
trace(Data);
if(++_done == _total) goFurther(); // this will only be called when all the events have been triggered
}