Last active
November 12, 2018 02:56
-
-
Save SamuelDavis/2d09d55a9b44ec2f4332ba6d2642cfdf to your computer and use it in GitHub Desktop.
A JavaScript timer which triggers a callback at a constant interval.
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
async function clock(actions, timeout) { | |
start = performance.now() | |
for (let i = 0; i < actions.length; i++) { | |
await actions[i]() | |
} | |
return setTimeout(clock.bind(clock, actions, timeout), timeout - (performance.now() - start)) | |
} | |
clock(new Array(3).fill(undefined).map((_, i) => () => { | |
console.log(i, performance.now()) | |
return new Promise(res => setTimeout(res, 500)) | |
}), 3000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment