What's wrong with the following code and when will it go wrong?
obj.addEventListener("event", Delegate.create(this, func));
Answer:
If you happen to call the code multiple times (say you enter the function more than once), then you are basically adding multiple delegate objects to the queue. From the surface, it looks like only one function has been set to listen to the event. But what actually happens is that you are adding multiple function object (aka delegate) to the event listening queue, and that will cause your event handler (func) to be called multiple times.
So the right way to do this would be to either make sure your addEventListener only gets called once, but if it has to be in a place where it might be called multiple times, set up a delegate table to save the delegate objects and use it in your Delegate.create call later.
e.g.
function init()
{
_handlerTable = new Array();
_handlerTable["onReleaseHandler"] = Delegate.create(this, onReleaseHandler);
_handlerTable["handleSomethingHandler"] = Delegate.create(this, handleSomethingHandler);
...
}
// function might be called multiple times
function initButtonStates()
{
obj.addEventListener("event", _handlerTable["onReleaseHandler"]);
...
}
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home