Bonus info; this only has to work in Safari, hence the missing ie support. For that we will use the 'arguments' variable, that is available in every function automatically. I recently ran into a problem involving the removeEventListener() method, which caused me a good half an hour of confusion before a lightbulb appeared above my head and I was enlightened by a solution — a solution which, it must be said, is very obvious in hindsight. Which equals operator (== vs ===) should be used in JavaScript comparisons? Mainly because “_func.name == undefined”. Does my toilet drain poorly because of bad venting? A version of Otto Nascarella‘s solution that works in strict mode is: May be several anonymous functions, keydown[1].
Why is macOS often referred to as 'Darwin'? The first parameter is the type of the event (like "click" or "mousedown" or any other HTML DOM Event. window.cpAPIEventEmitter.addEventListener("CPAPI_VARIABLEVALUECHANGED",runTimer,"cpInfoCurrentFrame"); window.cpAPIEventEmitter.removeEventListener("CPAPI_VARIABLEVALUECHANGED",doRemove2,"cpInfoCurrentFrame"); Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type. I have read in other questions that it is not possible to call removeEventListener on anonymous functions, but is this also the case in this situation? As of jQuery version 1.7, the off() method is the new replacement for the unbind(), die() and undelegate() methods. So doubtless many people know this already, but I’m recording it here along with another approach I thought of afterwards, in the hope that they may be useful to someone in the future. Can the molecular bond angle of water be changed via distillation? Now I want to stop the event listener in some cases. You can add many event … The final suggestion of assigning the function to a variable won’t work in all cases. How can I measure correctly with a ruler? What is the most efficient way to deep clone an object in JavaScript?
It looks like this: (there is a lot more code, but this is enough to show the problem). You must be able to a pass a reference to the removal method in order for the method to find and remove that listener. PS: I do not use jQuery.
December 24, 2017 To learn more, see our tips on writing great answers. If you didn't maintain a reference to the function object, then no. Anonymous functions, like "element.removeEventListener("event", function(){ … What does “use strict” do in JavaScript, and what is the reasoning behind it? Here's an example function.