Is it possible to remove an event listener from within the listener itself ?

Just wondering if it is possible to remove an event listener from within the listener itself.  I basically want to listen for an event.  When the event fires I want to execute some code, and then I want to remove/destroy the listener, from within the listener itself.  e.g.

ref.addEventListener(event, function() {
xxxx
xxxx
xxxx
ref.removeEventListener(event,function(){});
}

Open in new window


Is there a way to do that ?  I tried something like the above but it doesn't seem to work.
LVL 5
sscottiAsked:
Who is Participating?
 
Duy PhamFreelance IT ConsultantCommented:
It's possible to do that, but you have to use a named-function instead of inline function in order to let JS runtime to know exactly which handler to be removed.
Check an example here: http://jsfiddle.net/scc70sqm/.
0
 
sscottiAuthor Commented:
Thanks.  That works.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.