Search code examples
actionscript-3flashfor-loop

as3 loop loading urls - continue loop on complete


I am trying to load a bunch of files in order. I want the other to start downloading once the previous has downloaded. I thought the best way to do this would be through a for loop.

TheURL = is a bunch of urls in a ARRAY

for(var i:int=0;i<TheURL.length;i++)
{
    var urlString:String = TheURL[i];
    var urlReq:URLRequest = new URLRequest(urlString);
    var urlStream:URLStream = new URLStream();
    var fileData:ByteArray = new ByteArray();
    urlStream.addEventListener(Event.COMPLETE, loaded);
    urlStream.load(urlReq);

function loaded(event:Event):void
    {
        /// code to continue loop
    }

}

It is important that the others do not start downloading until the previous has completed. How can I do that?


Solution

  • function downloadFiles():void
    {
        downloadNextFile();
    }
    
    function downloadNextFile():void
    {
        var urlString:String = TheURL.shift();
        var urlReq:URLRequest = new URLRequest(urlString);
        var urlStream:URLStream = new URLStream();
        var fileData:ByteArray = new ByteArray();
        urlStream.addEventListener(Event.COMPLETE, loaded);
        urlStream.load(urlReq);
    }
    
    function loaded(event:Event):void
    {
         downloadNextFile();
    }