Sometimes, we want to remove event listener with JavaScript.
In this article, we’ll look at how to remove event listener with JavaScript.
How to remove event listener with JavaScript?
To remove event listener with JavaScript, we can use the removeListener
method.
For instance, we write
let clickCount = 0;
const onClick = (event) => {
clickCount++;
if (clickCount == 50) {
canvas.removeEventListener("click", onClick);
}
};
canvas.addEventListener("click", onClick);
to call removeListener
with 'click'
and onClick
to remove the click listener.
We add the click listener by calling addEventListener
with 'click'
and onClick
.
Conclusion
To remove event listener with JavaScript, we can use the removeListener
method.