Bill
Bill

Reputation: 19338

AS removeEventListener before or after An object been removed

Quick question; should I use removeChild() or removeEventListener() first?

Also, if I set an object to null, do I still need to remove event listeners from that object?

Upvotes: 0

Views: 388

Answers (2)

loxxy
loxxy

Reputation: 13151

"Remove event listeners before removeChild."

Whether doing the other way will throw an error or not, totally depends on the event you are listening to & the objects you are trying to access after the event fires.

But it always a good practice to remove event listeners as fast as you can.

For example : If you require the click of a button once & you seem to disable the button after click, It doesn't make sense to keep the listener running.


And Yes you do need to manually remove all event listeners. But you can also use weak event listeners:

addEventListener(MouseEvent.CLICK, clicked, false, 0, true);

Which hints the garbage collector to remove the listener when object is set to null.

Upvotes: 3

Marty
Marty

Reputation: 39466

Should I remove all the eventListener before removeChild, or removeChild before eventListener.

These tasks are unrelated, so it doesn't matter. That said, there are some events which are triggered by removing a child, so if you're relying on Event.REMOVED_FROM_STAGE, then you'll want to remove the event listener AFTER.

If I set an object to NULL, do I still need to removeEventListener?

Yes.

Upvotes: 1

Related Questions