Skip to content

Instantly share code, notes, and snippets.

@SamuelDavis
Last active November 12, 2018 02:56
Show Gist options
  • Save SamuelDavis/2d09d55a9b44ec2f4332ba6d2642cfdf to your computer and use it in GitHub Desktop.
Save SamuelDavis/2d09d55a9b44ec2f4332ba6d2642cfdf to your computer and use it in GitHub Desktop.
A JavaScript timer which triggers a callback at a constant interval.
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