Must we use removeEventListener() for all addEventListener()implementation (in Firefox)?

S

sylver

Hi,

Unregistering event listeners (or memory allocation) is a good
practice in programming, but do we need to use removeEventListener()
for all addEventListener() implementation?

Please consider this code:

if(choice == a)
button.addEventListener("click", function () {alert('just trying
anonymous function over here');}, true);
else
button.addEventListener("click", function () {alert('here's where
the else go');}, true);

As you can see, there are 2 different functions (or maybe more) that
may be attached to the button onclick event, depending on user's
choice, and they are going to invoke anonymous functions.

Questions:
===========
1) Previous searches in the google groups suggested that there aren't
a removeALLEventListener method? or is there?
2) If i remove the button, using either removeChild() method or by
closing the tab/window, will the event listener be removed as well?

Please help.

Thanks so much :D
 
P

pr

sylver said:
Unregistering event listeners (or memory allocation) is a good
practice in programming, but do we need to use removeEventListener()
for all addEventListener() implementation?

Please consider this code:

if(choice == a)
button.addEventListener("click", function () {alert('just trying
anonymous function over here');}, true);
else
button.addEventListener("click", function () {alert('here's where
the else go');}, true);

As you can see, there are 2 different functions (or maybe more) that
may be attached to the button onclick event, depending on user's
choice, and they are going to invoke anonymous functions.

Questions:
===========
1) Previous searches in the google groups suggested that there aren't
a removeALLEventListener method? or is there?

There isn't. You must call removeEventListener() with the same arguments
you used in addEventListener().

In simple cases, you can use arguments.callee to get a reference to the
running (including anonymous) function, making this kind of once-only
event listener possible:

document.addEventListener("click", function (e) {alert("ouch!");
document.removeEventListener("click", arguments.callee, false);
}, false); // only reacts to the first click

In more complex scenarios, you need to retain a reference to the
function supplied when you registered the listener. See <URL:
http://www.w3.org/TR/DOM-Level-2-Events>.

One of the many ways your example might work, for instance, is thus:

var listeners = [
function (e) {alert("first");},
function (e) {alert("second");}
];

button.addEventListener("click", listeners[choice], true);
...
button.removeEventListener("click", listeners[choice], true);

2) If i remove the button, using either removeChild() method or by
closing the tab/window, will the event listener be removed as well?

Hopefully yes; I don't know of any bugs or leaks - although I haven't
seen an IE implementation yet :)
 
T

Thomas 'PointedEars' Lahn

sylver said:
Unregistering event listeners (or memory allocation) is a good
practice in programming, but do we need to use removeEventListener()
for all addEventListener() implementation?

Please consider this code:

if(choice == a)
button.addEventListener("click", function () {alert('just trying
anonymous function over here');}, true);
else
button.addEventListener("click", function () {alert('here's where
the else go');}, true);

If an event listener is to be added anyway, why not consider the following:

button.addEventListener(
"click",
(function(a, b) {
if (a == b)
{
return function() {
window.alert('just trying anonymous function over here');
};
}
else
{
return function() {
window.alert('here's where the else go');
};
}
})(choice, a),
true);

BTW, is there a point to the event capturing (`true') or is it just the
result of copy-and-pray?
As you can see, there are 2 different functions (or maybe more) that
may be attached to the button onclick event, depending on user's
choice, and they are going to invoke anonymous functions.

Questions:
===========
1) Previous searches in the google groups suggested that there aren't
a removeALLEventListener method? or is there?

Well, there is window.location.reload(). Seriously: no, unless you
implement a registration of event listeners.
2) If i remove the button, using either removeChild() method or by
closing the tab/window, will the event listener be removed as well?

The event listener is a Function object. As with all objects, they are not
marked for garbage collection before there are no more references to it.
However, as with the event target a host object is involved, MSHTML may have
problems cleaning up the mess; see the FAQ Notes on closures. On the other
hand, IE up to version 7 does not support addEventListener() anyway.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top