Last active
February 15, 2021 02:33
-
-
Save electerious/6b7002baacdefc071a981cf796415268 to your computer and use it in GitHub Desktop.
Function that continuously executes the function it has been most recently called with
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 single = (max) => { | |
let id | |
let iterations | |
const loop = (_id, fn) => { | |
if (id!==_id) return | |
if (max!==undefined && iterations>=max) return | |
++iterations | |
fn() | |
requestAnimationFrame(() => loop(_id, fn)) | |
} | |
return (fn) => { | |
id = Symbol() | |
iterations = 0 | |
loop(id, fn) | |
return () => id = Symbol() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example: