0

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?

3 Answers 3

4
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();
}
Sign up to request clarification or add additional context in comments.

2 Comments

Damn, you were faster :D But didn't you mean shift() instead of unshift()?
Awesome! I will test it later today. Don't have time now. Thank you so much!!
2

Using a for-loop does not solve what you want to do.

In my opinion the easiest way should be to use the array of URLs as a queue. This can be done by using Array.shift(). But you should make a copy of your array if you need the original set of URLs when finished, because shift() makes an inline modification of the array.

The solution could be the following:

function loadQueue(urlQueue:Array):void
{
    var url:String = urlQueue.shift();
    var request:URLRequest = new URLRequest(url);
    var stream:URLStream = new URLStream();
    var data:ByteArray = new ByteArray();

    var completeHandler:Function = function(event:Event)
    {
        // remove listener from stream to be a clean coder ;)
        stream.removeEventListener(Event.COMPLETE, completeHandler);

        // handle completion in the way you need ...

        // continue with the next element
        if (urlQueue.length > 0)
           loadQueue(urls);
    }

    urlStream.addEventListener(Event.COMPLETE, loaded);
    urlStream.load(urlReq);
}

Loading your queue will then look like:

loadQueue(TheURL.concat()); // concat() will clone your array

2 Comments

I need a lil help with this. Have not got it to work yet but I love the concept. Do you have a moment? For example you posted. urlStream.load(urlReq); But in your code urlReg is gone. Am I missing something? Also will this code only "concat" with a comma? My array is separated by "|" - -loadQueue(TheURL.concat());
I didn't name the request urlReq. In my code it is simply called request. In general I didn't copy your code and changed it - I rewrote it. ;) The array: how can your Array be separated by '|' unless it's a string with '|' as separator? If TheURL is such a string, then use TheUrl.split("|") that will create an array of strings. When TheURL is an array already, then TheURL.concat() will simply creates a new array with the same elements of TheURL in the same order. API Doc of Array.concat()
0

You should use a library like greensock LoaderMax http://www.greensock.com/loadermax/.
It's much more difficult than it seems to create your own loading queue. There are plenty of bugs, traps and special cases to deal with. It seems to work fine, and one day a customer call you back because of a bug on this particular flash version on this computer behind this firewall... I've started to do mine library, but finally I used LoadManager.

1 Comment

Its called LoaderMax, not LoaderManager (I corrected my answer). You can find it there greensock.com/loadermax . Just download it and take some time to learn it before you use it. It's worth it. I've been able to create dynamic queue for browsing a gallery of 20000 images smoothly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.