34

Sorry if this is a common question, but I couldn't find any answers that seemed pertinent through searching.

If I attach an event listener like this:

window.addEventListener('scroll', function() { check_pos(box); }, false);

it doesn't seem to work to try to remove it later, like this:

window.removeEventListener('scroll', function() { check_pos(box); }, false);

I assume this is because the addEventListener and removeEventListener methods want a reference to the same function, while I've provided them with anonymous functions, which, while identical in code, are not literally the same.

How can I change my code to get the call to removeEventListener to work? The "box" argument refers to the name of an <iframe> that I'm tracking on the screen; that is, I want to be able to subscribe to the scroll event once for each <iframe> that I have (the quantity varies), and once the check_pos() function measures a certain position, it will call another function and also remove the event listener to free up system resources.

My hunch is that the solution will involve a closure and/or naming the anonymous function, but I'm not sure exactly what that looks like, and would appreciate a concrete example.

Hope that makes sense.

Brian Tompsett - 汤莱恩
5,92972 gold badges63 silver badges135 bronze badges
asked Jun 7, 2010 at 16:42

3 Answers 3

29

Have you tried maintaining a reference to the anonymous function (like you suggested)?

So:

var listener = function() {
 check_pos(box);
};
window.addEventListener('scroll', listener, false);
...
window.removeEventListener('scroll', listener, false);

Mozilla's docs suggest the same thing.

cbalos
9091 gold badge9 silver badges19 bronze badges
answered Jun 7, 2010 at 16:47

3 Comments

Thanks, Vivin - correct me if I'm wrong, but I don't think this would allow me to set up multiple event listeners with different values for "box," which is what I need to do. The argument "box" isn't a global variable. For example, just using literal values, I might call window.addEventListener('scroll', function() { do_something('string1'); }, false); and later window.removeEventListener('scroll', function() { do_something('string1'); }, false); and later window.addEventListener('scroll', function() { do_something('string2'); }, false);, etc.
There is a way to add multiple event listeners by using closures (See stackoverflow.com/questions/2276961/…). However I'd suggest using jQuery if you want to do some serious event-handling. It makes it much easier.
If remove it by using < window.removeEventListener('scroll', listener, false); > and check again by using < "listener" in window" > so it is still giving me back "true". Any help?
4
var listener;
listener = function(){
if( window.target != anotherEvent.target )
{
 ...CODE where
 window.removeEventListener('click', listener , false);
};
window.addEventListener('click', listener ,false);
Daniel
23.4k12 gold badges111 silver badges155 bronze badges
answered Oct 15, 2012 at 20:52

Comments

0

document.getElementById("yourId").removeEventListener("click",yourfunction1);
document.getElementById("yourId").addEventListener("click",yourfunction2);
function yourfunction1(){
//write code here
alert(1);
}
function yourfunction2(){
//write code here
alert(2);
}
<button type="button" onclick="yourfunction1()" id="yourId">Button</button>

answered Jul 16, 2019 at 17:12

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.