Reputation: 2027
Some of our legacy code creates listeners with on
and anonymous functions. These listeners have a finite lifespan.
Code is something like this:
me.getEl().on(someCondition()? 'mousedown': 'touchstart',
function(event) {
if (someOtherCondition()) {
destroyStuff();
// We don't need this listener any more!
// ***
}
}
);
Checking the resource usage as the application runs, these zombie listeners accumulate and, especially the mouseover ones, use up a lot of resources and cycles.
Is there a way to use un
or removeEventListener
below the // ***
comment and remove the listener? Say something along these lines:
event.target.removeEventListener(event.type, /* reference this anonymous function? */);
Upvotes: 0
Views: 334
Reputation: 20234
You have multiple options.
on
takes an options
parameter that may contain single:true
if it should only be executed once. I think this would not work for your example because the listener can be called multiple times, but possibly for some of your use cases that do not have a condition.mon
with destroyable: true
to create destroyable listeners. These listeners can be removed using mun
.Upvotes: 3