View Full Version : AS3 Delay a for loop
itbkris
February 9th, 2010, 11:56 AM
I'm writing an upload program to upload multiple files. It works great as long as I only upload one file but as soon as I add a second file it throws an error saying it can only perform one upload at a time. Is there a way I can have it not loop back through the for loop until the upload has completed?
Here is a chunk of my code
for(var q:int = 0; q < selectedFileArray.length; q++)
{
trace("Loop marker 1");
fr.addEventListener(Event.COMPLETE, completeHandler);
fr.addEventListener(IOErrorEvent.IO_ERROR, function(e:IOErrorEvent):void{ trace(e) });
fr = FileReference(selectedFileArray[q]);
fullfile = fr.name;
filesplit = fullfile.split(".");
fileext = filesplit[1];
var uprequest:URLRequest = new URLRequest("www.myaddy.com");
fr.upload(uprequest);
function completeHandler(event:Event):void{ trace("Upload complete"); }
}
milkmit
February 9th, 2010, 12:24 PM
Maybe this is a bit of a hacked solution, but couldn't you simply define a variable prior to loading your first one whose value is selectedFileArray.length, and then in the Event.COMPLETE handler for the loaded file do a (numberOfFiles--), and then finally do another check to see if numberOfFiles > 0, and if so load the next one?
And for how to reference each file each time you go through this sorta hacked loop, you could simple use selectedFileArray[numberOfFiles-1], which should cycle through each one as to decrement the value of numberOfFiles each time.
Does that make sense? Maybe there's a more efficient way, but I think that's how I might try it....and in fact, I'm shortly going to be working on a similar file-loading queue and I wasn't quite sure how to approach it, but I think this helped me think it through "out loud". ;)
creatify
February 9th, 2010, 12:30 PM
I didn't test, but something like the following. Avoid the loop all together, that's what the complete handlers are for in this case. Also, not sure how you've instantiated fr - but you only need to add the listeners once. Also, I'm not sure what this is, but the constructor doen't take arguments? fr = FileReference(selectedFileArray[num]);
// avoid the loop
var total:int = selectedFileArray.length;
var q:int = 0;
fr.addEventListener(Event.COMPLETE, completeHandler);
fr.addEventListener(IOErrorEvent.IO_ERROR, function(e:IOErrorEvent):void{ trace(e) });
function startLoad(num:int):void {
fr = FileReference(selectedFileArray[num]);
fullfile = fr.name;
filesplit = fullfile.split(".");
fileext = filesplit[1];
var uprequest:URLRequest = new URLRequest("www.myaddy.com");
fr.upload(uprequest);
}
function completeHandler(event:Event):void {
trace("Upload complete: "+q);
q++;
if(q<total) {
startLoad(q);
} else {
trace("all loaded");
}
}
itbkris
February 9th, 2010, 12:54 PM
That function works but it seems liek my file array only adds the last file so i'm fixing that. I'll post the results when done. Thanks for the help.
milkmit
February 9th, 2010, 01:48 PM
Yay, looks like I was on the right track myself but yes, mine would load backwards. Thanks, creatify!
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.