Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to removeEventListener that is addEventListener with anonymous function?

function doSomethingWith(param) {     document.body.addEventListener(         'scroll',         function()         {             document.write(param);         },         false     ); // An event that I want to remove later } setTimeout(     function()     {         document.body.removeEventListener('scroll', HANDLER ,false);             // What HANDLER should I specify to remove the anonymous handler above?     },     3000 ); doSomethingWith('Test. '); 
like image 538
Japboy Avatar asked Apr 14 '11 07:04

Japboy


People also ask

How do I get rid of anonymous event listener?

Strictly speaking you can't remove an anonymous event listener unless you store a reference to the function. Since the goal of using an anonymous function is presumably not to create a new variable, you could instead store the reference in the element itself: element. addEventListener('click',element.

How do you remove the event listener from a function?

The removeEventListener() is an inbuilt function in JavaScript which removes an event handler from an element for a attached event. for example, if a button is disabled after one click you can use removeEventListener() to remove a click event listener.

How do you declare an anonymous function?

An anonymous function can also have multiple arguments, but only one expression. We may also declare anonymous function using arrow function technique which is shown below: ( () => { // Function Body... } )

Why are anonymous functions frequently used with event handlers?

More generally, the point of using anonymous functions is that they do not require a name, because they are "event handlers" bound to a specific event on a specific object. In this case, the object is the entire Document Object Model, and the anonymous function executes when the entire DOM has loaded.


2 Answers

You can't. You have to use a named function or store the reference somehow.

var handler;  function doSomethingWith(param) {     handler = function(){         document.write(param);     };       document.body.addEventListener('scroll', handler,false); } setTimeout(function() {      document.body.removeEventListener('scroll', handler ,false); }, 3000); 

The best would be to do this in a structured way, so that you can identify different handlers and remove them. In the example above, you obviously could only remove the last handler.

Update:

You could create your own handler handler (:)) :

var Handler = (function(){     var i = 1,         listeners = {};      return {         addListener: function(element, event, handler, capture) {             element.addEventListener(event, handler, capture);             listeners[i] = {element: element,                               event: event,                               handler: handler,                               capture: capture};             return i++;         },         removeListener: function(id) {             if(id in listeners) {                 var h = listeners[id];                 h.element.removeEventListener(h.event, h.handler, h.capture);                 delete listeners[id];             }         }     }; }()); 

Then you can use it with:

function doSomethingWith(param) {     return Handler.addListener(document.body, 'scroll', function() {         document.write(param);     }, false); }  var handler = doSomethingWith('Test. ');  setTimeout(function() {      Handler.removeListener(handler); }, 3000); 

DEMO

like image 75
Felix Kling Avatar answered Oct 05 '22 22:10

Felix Kling


You can't, you need a reference to the function:

function doSomethingWith(param) {    var fn = function(){ document.write(param); };    document.body.addEventListener('scroll', fn, false);    setTimeout(function(){ document.body.removeEventListener('scroll', fn, false); }, 3000); } doSomethingWith('Test. '); 
like image 34
davin Avatar answered Oct 05 '22 22:10

davin