Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

removeEventListener is not working

I don't know what I am doing wrong but here is an example of what I am doing and it doesn't seem to work.

someDom.addEventListener('mousemove',function(ev) {self.onInputMove(ev)},false);

someDom.removeEventListener('mousemove',self.onInputMove);

The removeEventListener code is executed but it just doesn't remove the 'mousemove' listener

like image 309
fogy Avatar asked Apr 28 '11 21:04

fogy


People also ask

How do I know if removeEventListener is working?

After your call to removeEventListener is made, go back and check your event listeners again. If it was successful, your event listener should no longer be set.

Why is my event listener not working?

If your event listener not working is dependent on some logic, whether it's about which element it'll listen on or if it's registered at all, the first step is to check that the listener is indeed added to the element. Using a breakpoint in the developer tools , a logpoint or console.

Do you need to removeEventListener?

If there is no memory leak, the used memory will increase by around 1000kb or less after the tests are run. However, if there is a memory leak, the memory will increase by about 16,000kb. Removing the event listener first always results in lower memory usage (no leaks).

Why is addEventListener not a function?

addeventListener is not a function", which means that the function we're calling is not recognized by the JavaScript interpreter. Often, this error message actually means that we've spelled something wrong. If you are not sure of the correct spelling of a piece of syntax, it is often good to look up the feature on MDN.


4 Answers

removeEventListener removes the listener that exactly matches the function that was added.

In this case, the function that addEventListener added was:

var some_func = function(ev) {
    self.onInputMove(ev);
};

Store a reference to the actual function and you'll be good. So for example, the following should work:

someDom.addEventListener('mousemove',self.onInputMove,false);

someDom.removeEventListener('mousemove',self.onInputMove,false);
like image 185
Sean Vieira Avatar answered Oct 13 '22 11:10

Sean Vieira


onInputMove is not an event-callback method. So you need to do something like:

var event = function(ev) {self.onInputMove(ev)};
someDom.addEventListener('mousemove', event,false);

someDom.removeEventListener('mousemove', event, false);
like image 37
cem Avatar answered Oct 13 '22 11:10

cem


Why make it yourself so hard, just use the following to bind an event to an element:

element.onmousemove = function(e) {
    // Some code here...
    alert("Mouse moved!");
};

Now, when you want to remove the event, just do this:

element.onmousemove = null;

Done!

Hope this helps you guys out!

like image 20
Steffen Brem Avatar answered Oct 13 '22 13:10

Steffen Brem


This page comes first on searching this/such issue on Google. So apart from answers already mentioned, here is one more interesting fact for future:

Leaving out the third optional variable in addEventListener() for useCapture/useBubble (as it defaults to false) does create some issue while removing the same eventlistener with same callback name. I faced this issue while working on chrome. Cant say about other browsers.

So do mention the third variable explicitly as "false".

like image 38
daksh_019 Avatar answered Oct 13 '22 11:10

daksh_019