Skip to content

Instantly share code, notes, and snippets.

@7350206
Created March 28, 2022 11:41
Show Gist options
  • Save 7350206/bc72c3f1663da14161bc13d58ff79380 to your computer and use it in GitHub Desktop.
Save 7350206/bc72c3f1663da14161bc13d58ff79380 to your computer and use it in GitHub Desktop.
LYeyQVO
<button id="btn" onClick="once(doStuff, res2)(1,2,3)">1</button>
<button id="btn" onClick="doStuffOnce(1,2,3)">1-ok</button>
<button id="btn" onClick="once(res1, res2)(100)">2</button>
<button id="btn" onClick="resOnce(100)">2-ok</button>
const doStuff = () => {
alert(`done`);
};
const res1 = (a) => console.log(a, "done");
const res2 = (b) => console.log(b, "dont ever think");
const once = (fn1, fn2) => {
let done = false;
return (...args) => {
if (!done) {
fn1(...args);
done = true;
} else {
fn2(...args);
}
};
};
const doStuffOnce = once(doStuff, res2);
const resOnce = once(res1, res2);
#btn {
padding: 10px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment