Last active
February 27, 2022 17:21
-
-
Save SanariSan/ea93c004e369d5ec0b61a141a3cd41bd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
<div id="foo">123</div> | |
<button onClick="trigger();">toggle</button> | |
*/ | |
const once = (el, eventName, handler) => el.addEventListener(eventName, handler); | |
const onceHandlerWrap = (fn, eventName) => { | |
const _handler = (e, ...args) => { | |
fn(...args); | |
e.target.removeEventListener(eventName, _handler); | |
}; | |
return _handler; | |
} | |
// ------ | |
const customHandler = (...args) => console.log(true, [...args]); | |
const trigger = () => { | |
let el = document.querySelector("#foo"); | |
once(el, "click", onceHandlerWrap(customHandler, "click")); | |
} | |
/* | |
or just | |
document.body.addEventListener('click', cb, { once: true }); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment