Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove all event listeners from a display object?

Is there a way to determine which event listeners are registered with a display object? I want to remove all event listeners from a display object so that I can assign new ones based on context changes in the application.

like image 913
Soviut Avatar asked Sep 21 '09 03:09

Soviut


People also ask

Is there a way to remove all event listeners?

To remove all event listeners from an element: Use the cloneNode() method to clone the element. Replace the original element with the clone. The cloneNode() method copies the node's attributes and their values, but doesn't copy the event listeners.

Which mode allows you to remove all event listeners from an element?

You can remove all event listeners from a DOM element in Javascript by replacing the element with a deep clone of itself. elem. cloneNode(...) will not clone the event listeners of the source element.

How do you remove all event listeners react?

Add the event listener in the useEffect hook. Return a function from the useEffect hook. Use the removeEventListener method to remove the event listener when the component unmounts.

Should you always remove event listeners?

The event listeners need to be removed due to following reason. Avoid memory leaks, if the browser is not handled it properly. Modern browsers will garbage collect event handlers of removed DOM elements but it is not true in cases of legacy browses like IE which will create memory leaks.


2 Answers

back2dos has mentioned the approach you should use, what i did was extend the movieclip class and implemented all kinds of functions that i use on daily basis but are not part of the movieclip class. including the override for the addEventListener class

protected var listeners : Dictionary    = new Dictionary();
override public function addEventListener( type : String, listener : Function, useCapture : Boolean = false, priority : int = 0, useWeakReference : Boolean = true) : void
{
        var key : Object = {type:type,useCapture:useCapture};
        if( listeners[ key ] ) {
                removeEventListener( type, listeners[ key ], useCapture );
                listeners[ key ] = null;
        }
        listeners[ key ] = listener;

        super.addEventListener( type, listener, useCapture, priority, useWeakReference );
}
protected function removeListeners () : void
{
        try
        {
            for (var key:Object in listeners) {
                    removeEventListener( key.type, listeners[ key ], key.useCapture );
                        listeners[ key ] = null;
            }
        }catch(e:Error){}
}
like image 163
Andy Jacobs Avatar answered Oct 05 '22 04:10

Andy Jacobs


jeceuyper is right ...

a side not though: DisplayObject extends EventDispatcher, which already does implement IEventDispatcher ... so to be more precise: you need to override addEventListener and removeEventListener to keep track of the listeners ...

a few technical details: i suggest you use Dictionary to store the handler functions ... a bit slower for insertion, but much faster for removal ... also, Dictionary supports weak references, which is quite important in the case of event handling ... also keep in mind, that useCapture allows to add the same handler twice ...

good luck then ... ;)

like image 29
back2dos Avatar answered Oct 05 '22 02:10

back2dos