Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I know that removeEventListener was successful?

Tags:

Is there a way to confirm that removeEventListener was successful? I'm using it on a video element for the "playing"/"pause" event. The behavior I'm trying to achieve seems to be inconsistent. Here is an example of how I'm using it.

var playListener = function(){
  console.log("video is playing") 
}

videoElement.addEventListener("playing", playListener)

Somewhere else in my code

videoElement.removeEventListener("playing", playListener)

playListener is available where I'm using removeEventListener and I'm passing around the video element's id so it is the same videoElement I added the event listener to.

I want to know if I can do some kind of console.log or something to confirm that the removeEventListener worked.

like image 875
Ephapox Avatar asked Dec 04 '15 01:12

Ephapox


People also ask

How do I check if an event listener exists?

To check whether dynamically attached event listener exists or not with JavaScript, we can get and set the element's listener attribute. export const attachEvent = ( element: Element, eventName: string, callback: () => void ) => { if (element && eventName && element. getAttribute("listener") !== "true") { element.

Why do we need removeEventListener?

The removeEventListener() is an inbuilt function in JavaScript which removes an event handler from an element for a attached event. for example, if a button is disabled after one click you can use removeEventListener() to remove a click event listener.

Are event listeners removed when element is removed?

According to the jquery Documentation when using remove() method over an element, all event listeners are removed from memory. This affects the element it selft and all child nodes. If you want to keep the event listners in memory you should use . detach() instead.

How do I close an event listener?

Event listeners can also be removed by passing an AbortSignal to an addEventListener() and then later calling abort() on the controller owning the signal.


1 Answers

Unfortunately, the removeEventListener method doesn't return anything or throw an error when it does not successfully remove an event listener. There's also no JavaScript method to access what event listeners are currently set for a given element.

As far as debugging your issue, most modern browser's developer tools do provide the capability to view event listeners. You can put a debugger statement in your code (or set a breakpoint) to pause execution immediately before you've called removeEventListener:

debugger;
videoElement.removeEventListener("playing", playListener);

Once the breakpoint is reached and execution is paused, check the event listeners from Chrome Dev Tools' Elements tab to verify that your event listener is currently set:

Chrome Dev Tools Event Listners

You can also find event listeners in Firefox Dev Tools' Inspector tab:

Firefox Dev Tools Event Listeners

After verifying that your event listener is currently set, go back to the debugger (Chrome "Sources" Tab / Firefox "Debugger" Tab) and step through the code (F10) line by line.

After your call to removeEventListener is made, go back and check your event listeners again. If it was successful, your event listener should no longer be set. Once you're done debugging, you can then resume code execution (F8).

like image 54
nderscore Avatar answered Oct 23 '22 05:10

nderscore