Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 171121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:55:52+00:00 2026-05-11T12:55:52+00:00

I have a weird quirk in ActionScript. I need to pass the index to

  • 0

I have a weird quirk in ActionScript. I need to pass the index to a callback function.

Here is my code

for (var i:Number = 0; ((i < arrayQueue.length) && uploading); i++) {     var lid:ListItemData=ListItemData(arrayQueue[i]);     var localI:Number= new Number(i); // to copy?     var errorCallback:Function = function():void { OnUploadError(localI); };     var progressCallback:Function = function(e:ProgressEvent):void { lid.progress = e; OnUploadProgress(localI); };     var completeCallback:Function = function():void { Alert.show('callback'+localI.toString()); OnUploadComplete(localI); }; // localI == arrayQueue.length - 1 (when called)     Alert.show(localI.toString());  // shows current i as expected     lid.fileRef.addEventListener(Event.COMPLETE, completeCallback);     lid.fileRef.addEventListener(ProgressEvent.PROGRESS, progressCallback);     lid.fileRef.addEventListener(HTTPStatusEvent.HTTP_STATUS, errorCallback);     lid.fileRef.addEventListener(IOErrorEvent.IO_ERROR, errorCallback);     lid.fileRef.addEventListener(SecurityErrorEvent.SECURITY_ERROR, errorCallback);      lid.fileRef.upload(url, 'File'); } 

Any idea on how to pass in the index to my callbacks? .upload does not block.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T12:55:53+00:00Added an answer on May 11, 2026 at 12:55 pm

    Passing additional parameters for your callbacks is possible via some kind of delegate function or closure. However it is often considered a bad practice. You may use event target property instead to determine your index based on FileReference.

    Edit: here is a sample of using closures:

    function getTimerClosure(ind : int) : Function {     return function(event : TimerEvent) {         trace(ind);     }; }  for (var i = 0; i < 10; i++) {     var tm : Timer = new Timer(100*i+1, 1);     tm.addEventListener(TimerEvent.TIMER, getTimerClosure(i));     tm.start(); } 

    This will continuously trace numbers from 0 to 9.

    Edit2: here is a sample of creating a delegate based on a function closure:

    function timerHandler(event : Event, ...rest) : void {     trace(event, rest); }  function Delegate(scope : Object, func : Function, ...rest) : Function {     return function(...args) : void {         func.apply(scope, args.concat(rest));     } }  var tm : Timer = new Timer(1000, 1); tm.addEventListener(TimerEvent.TIMER, Delegate(this, this.timerHandler, 1, 2, 3)); tm.start(); 

    However this is a bad approach since unsubscribing for such a listener is a hell pain. This in turn will probably cause some memory leakages, which will decrease overall performance of your application. So, use with caution!


    Bottom line: if you know how to work with closures, use them – it is a wonderful thing! If you don’t care about your application performance in a long perspective, use closures – it’s simple!

    But if you are unsure about closures, use a more conventional approach. E.g. in your case you could create a Dictionary that matches your FileReference objects to appropriate indices. Something like that:

    var frToInd : Dictionary = new Dictionary(false); // false here wouldn't prevent garbage collection of FileReference objects  for (var i : int = 0; i < 10; i++) {     // blah-blah stuff with `lib` objects     frToInd[lib.fileRef] = i;     // another weird stuff and subscription  }  function eventListener(event : Event) : void {     // in the event listener just look up target in the dictionary     if (frToInd[event.target]) {         var ind : int = frToInd[event.target];     } else {         // Shouldn't happen since all FileReferences should be in          // the Dictionary. But if this happens - it's an error.     } } 

    — Happy coding!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen some very weird for loops when reading other people's code. I
I'm having a weird problem, I have the following code in my page.php template
I have a weird date rounding problem that hopefully someone can solve. My client
I have a weird issue that only seems to be affecting IE 7. The
I have a weird error in my C++ classes at the moment. I have
We have a weird intermittent problem with saving from Word 2007 to our SharePoint
i have a weird problem. i would like to delete an assembly(plugin.dll on harddisk)
I have a weird problem with images in visual web developer, I cant change
We have a semi-weird issue for our site. Consistently, from 1 anonymous* user the
I have a very weird problem: sometimes when I call nHibernate update to an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.