This video is available to students only

Destroying d3 event listeners

We can't let our event listeners hang around forever - we learn how to clean up after ourselves and cancel old ones.

Destroying d3 event listeners#

Before we look at adding events to our charts, let's learn how to destroy our event handlers. Removing old event listeners is important for updating charts and preventing memory leaks, among other things.

Let's add a 3 second timeout at the end of our code so we can test that our mouse events are working before we destroy them.

Removing a d3 event listener is easy — all we need to do is call .on() with null as the triggered function.

 

This page is a preview of Fullstack D3 Masterclass

Please select a discussion on the left.