EventTarget.addEventListener()
to add an event listener to an element.{ once: true }
as options to only run the given callback once.const listenOnce = (el, evt, fn) =>
el.addEventListener(evt, fn, { once: true });
listenOnce( document.getElementById('my-id'), 'click', () => console.log('Hello world') ); // 'Hello world' will only be logged on the first click
Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️