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

  • SEARCH
  • Home
  • 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 858435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:32:37+00:00 2026-05-15T08:32:37+00:00

I have a strange issue! I am trying to remove an event listener on

  • 0

I have a strange issue! I am trying to remove an event listener on a FileReference object by calling a function, but it seems not to be removed, and I do not understand why.

Here is the code:

private function clearFileUploadListeners(file:FileReference, index:String):void {
    var dispatchEvent:Function = function(event:Event):void {
        dispatch(event.type, event, index);
    };

    file.removeEventListener(Event.COMPLETE, dispatchEvent);
    var bool:Boolean = file.hasEventListener(Event.COMPLETE);
    if (bool)
        trace("ERROR");
}

When I run this code, the trace actually happens. I don’t understand why this boolean returns true, when I just tried to remove the eventListener just above! I guess I am probably doing something really stupid because it seems like a strange error.

I hope someone can please help me on this issue.

EDIT:

I believe it has to do with the fact that the dispatchEvent function is defined inside another function when I add the listener:

private function upload(file:FileReference, index:String):void {
    var dispatchEvent:Function = function(event:Event):void {
        dispatch(event.type, event, index);
    };

    file.addEventListener(Event.COMPLETE, dispatchEvent);
}

The problem is that I need to access this “index” variable from the listener, and I can’t set it as a global variable as each file has it’s own index and it’s a burden if I have to extend each event class to keep track of the index (Event, ProgressEvent, ..). I hope someone can please help me on this.

EDIT2:

I actually found a temporary solution, I am not sure if it is the best! I put my removeListener method actually inside the upload method, but made it a variable. As AS3 allows dynamic object, I attached this method to one of my object, and so I just call the reference to the method when necessary. The event is actually removed. Is this a good solution please?

Thank you very much,
Rudy

  • 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. Editorial Team
    Editorial Team
    2026-05-15T08:32:37+00:00Added an answer on May 15, 2026 at 8:32 am

    You’re right, it has to do with the fact that you’re defining a function inside another function, then using it to handle events.

    Each time the function upload is called, it creates a new closure, and assigns a reference to it to the dispatchEvent variable, which is then passed to the addEventListener class. So each time upload is called, it is using a new, different closure in the call to addEventListener. Similarly, in the clearFileUploadListeners function, a new closure is being created on each call (which happens to have the same code each time, but isn’t the same function object). The call to removeEventListener does nothing if the given callback has not been added as an event listener for the given event, which is the case here.

    To solve your problem, you need to store a reference to the closure that you pass to the addEventListener function. This way, you can get a reference to the same closure that was added when you need to remove it later in clearFileUploadListeners.

    You can try something along the lines of the following code (untested):

    import flash.utils.Dictionary;
    
    var callbackRegistry:* = new Dictionary();
    
    
    private function upload(file:FileReference, index:String):void {
        var dispatchEvent:Function = generateFileUploadCompleteCallback();
    
        callbackRegistry[file] = dispatchEvent;
    
        file.addEventListener(Event.COMPLETE, dispatchEvent);
    }
    
    private function clearFileUploadListeners(file:FileReference, index:String):void {
        var dispatchEvent:Function = callbackRegistry[file];
        callbackRegistry[file] = null;
    
        file.removeEventListener(Event.COMPLETE, dispatchEvent);
    
        var bool:Boolean = file.hasEventListener(Event.COMPLETE);
        if (bool)
            trace("ERROR");
        else
            trace("YAY, ALL OK!");
    }
    
    private function generateFileUploadCompleteCallback(index:String):Function {
        return function(event:Event):void {
            dispatch(event.type, event, index);
        };
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I followed the git guide but I have this strange issue when trying to
So i have this rather strange issue, i am trying to line up some
I have a strange issue with jQuery's hover, addClass $(document).ready(function(){ $('#selectable li').hover( function(){ $(this).addClass('selecting',
I have a strange issue. I'm trying to write a simple php webpage on
I have a strange issue. I'm trying to get Apple Push Notifications working with
I have a strange issue using HttpWebRequest, I'm trying to post a string to
I have a strange issue trying to run the Facebook iOS SDK DemoApp (from
I have a strange issue with a Widget that I am trying to create.
I have a strange issue: I am using SPContext.Current.Web in a .aspx page, but
I am trying to debug a strange issue with users that have LogMeIn installed.

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.