Created
March 28, 2022 11:41
-
-
Save 7350206/bc72c3f1663da14161bc13d58ff79380 to your computer and use it in GitHub Desktop.
LYeyQVO
This file contains hidden or 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
| <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> |
This file contains hidden or 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
| 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); |
This file contains hidden or 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
| #btn { | |
| padding: 10px; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment