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);