There is a problem with how we
stop the actions of an event listener.
We use a counter to stop the actions
after a set count. But
even though we don't do anything,
there is still an event listener on the
element.
You can see that it doesn't feel correct
to listen to an event and then do
nothing. Instead, a simpler approach
would be to stop listening to the event
itself.
We can use removeeventlistener()
for this:
The removeEventListener()
method will
need two parameters. One,
it needs to know which event
needs to be removed.
And second, it needs the name
of the function
that needs to be removed. This
is why, you need
to pass a named function when
adding an event listener.
Otherwise, when you want to remove
the event listener,
you'll have no way to do it.