Today I learned about an option which might come in handy sometime, so I’m jotting it down here.
It allows us to add a listener to an EventTarget which get’s removed automatically once it fired. So instead of manually removing the listener again
const eventCallback = () => {
eventTarget.removeEventListener("click", eventCallback);
};
const eventTarget = document.getElementById("myId");
eventTarget.addEventListener("click", eventCallback);
we can just pass the once
option:
const eventCallback = () => {
};
const eventTarget = document.getElementById("myId");
eventTarget.addEventListener("click", eventCallback, {once: true});