Repeat the number of event listeners?

In as3, if I add the same event listeners to the object, for example

txtField.addEventlistener( Event.CHANGE, changeCb, false, 0, true ); txtField.addEventlistener( Event.CHANGE, changeCb, false, 0, true ); 

Would I need to remove this listener twice?

How can I get a list or number of event listeners on an object?

+4
source share
2 answers

No, you do not need to remove the listener twice in this situation.

You need to remove multiple listeners in two situations:

  • if you add two event listeners with different listening functions:
     txtField.addEventlistener (Event.CHANGE, changeCb, false, 0, true);
     txtField.addEventlistener (Event.CHANGE, changeCb2, false, 0, true);

  1. if you set one event to record at the capture stage:
     txtField.addEventlistener (Event.CHANGE, changeCb, false, 0, true);
     txtField.addEventlistener (Event.CHANGE, changeCb, true, 0, true);

Therefore, you only need to delete events registered in another way.

You cannot count the number of event listeners with what is provided in a block in Flex, but you can check if it has an event listener for a specific type of event using hasEventListener(type) .

However, since the source code, if provided, you can "Monkey patch" for the UIComponent or FlexSprite class to add this functionality, as described in this blog . In fact, you do not even need to do this. The code is given in the example. Pretty awesome.

+7
source

No, you will not need to delete twice. You would create only one registration. In addition, you use weak links (setting the last parameter, useWeakReferences is true). So it's even easier to reason.

There is a section in the documents that describes cases where you would create two listener registrations for the same listener function.

http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/events/EventDispatcher.html#addEventListener ()

+2
source

All Articles