![Create a one time event handler](https://javascriptsource.com/wp-content/uploads/2021/08/Create-a-one-time-event-handler-702x526.jpg)
Use the once option
When attaching a handler to a given event, you can pass { once: true }
to the last parameter of the addEventListener
method:
const handler = function(e) {
// The event handler
};
ele.addEventListener('event-name', handler, { once: true });
Note that this option isn’t supported in IE.
Self-remove the handler
const handler = function(e) {
// The event handler
// Do something ...
// Remove the handler
e.target.removeEventListener(e.type, handler);
};
ele.addEventListener('event-name', handler);