I have the following code to add eventListener
area.addEventListener('click',function(event) {
app.addSpot(event.clientX,event.clientY);
app.addFlag = 1;
},true);
It is working correctly as expected..Later in another function i tried to remove the event listener using the following code
area.removeEventListener('click',function(event) {
app.addSpot(event.clientX,event.clientY);
app.addFlag = 1;
},true);
But the even listener is not removed..Why is it happening?Is there any problem with my removeEventListener()? Note:Here area is something like document.getElementById('myId')
I find that for the windows object, the last param "true" is required. The remove doesn't work if there is no capture flag.
This is because that two anonymous functions are completely different functions. Your
removeEventListener
's argument is not a reference to the function object that was previously attached.You are creating two different functions in both calls. So the second function does not relate in any way to the first one and the engine is able to remove the function. Use a common identifier for the function instead.
later you can then remove the handler by calling
If you want to pass local variables to the function called by the event listener, you can define the function inside the function (to get the local variables) and pass the name of the function in the function itself. For example, let's start inside the function that adds the event listener with app as a local variable. You would write a function inside this function such as,
Then you have what you need to remove it when waitExecute is called.
I've encountered a problem with removeEventListener() that needs explaining.
I wanted to be able to pass parameters to event listeners, so I wrote a function to generate the event listener, that in turn returns a second function, which calls my intended event listener as a callback.
The complete library file is as follows:
Then I wrote a quick test page to find out if this worked as intended, and allowed me to add AND remove event handlers at will.
The HTML test page is as follows:
For completeness, I use the following simple CSS file as well:
The test code is as follows:
So, the test to be performed is as follows:
[1] Attach a click event handler to Button #1;
[2] Test to see if the event handler is invoked when I click on the button;
[3] Once that test is passed, click on Button #2, and invoke the event handler attached thereto, which removes the old event handler attached to Button #1, then replaces it with a new event handler.
Steps [1] and [2] work fine. The event handler is attached, and invoked whenever I click upon the button.
The problem is with Step [3].
Even though I save a reference to the function generated by MakeEventHandler(), specifically for the purpose of removing that event listener in Step [3], the call to removeEventListener() does NOT remove the event listener. Subsequent clicking on Button #1 fires BOTH event listeners, including the one I supposedly removed!
Needless to say, this behaviour I find puzzling, despite carefully setting everything up so that the function I specify in the call to removeEventListener() is the self same function I added initially with addEventListener() - according to all the documentation on the subject I've read (including this thread), passing a reference to the same function for each call should work, but clearly doesn't.
At Step [1], the test output in the console reads, as expected:
The code is also run, as expected, in Step [2], and a step by step trace of the code reveals that indeed, the code is executed as expected.
But in Step [3], whilst the first click on Button #1 yields the desired result:
what happens when Button #1 is clicked upon subsequently is this:
Surely, even if the function initially attached to Button #1 still persists in memory, because it was generated within a closure, it should still be detached from the event listener collection for the element? Why is it still connected?
Or have I encountered some weird bug involving using closures with event listeners, that needs to be reported?
To remove it, store the function in a variable or simply use a named function and pass that function to the
removeEventListener
call: