ShakaCode | ShakaCode Blog | Rails On Maui Blog | Rails | ReactJs | JavaScript | Webpack | Productivity |

Event Listener Leaks


#1

I ran across some code where window resize event listeners added in a jQuery extension were never cleaned up.

Confirm how many event listeners there are.

If you’re using a newer version of jQuery, the magic command from the console is

$._data(window, "events")

Here’s an image of what I saw in the console when clicking around. This confirmed my hunch that I needed to add some code to clean up the prior window listener.

The fix to the code was was that I needed to be sure to remove the last listener before adding another one. I won’t go into those details, as those are specific to every situation. However, being able to track how many listeners you have is a critical ability to ensure that you don’t have a leak.

Chrome getEventListeners(window) doesn’t seem to help

Why doesn’t getEventListeners(window) provide me the relevant data?