Skip to content

Instantly share code, notes, and snippets.

@mendes5
Last active April 3, 2017 04:04
Show Gist options
  • Select an option

  • Save mendes5/57c0794bb9e17b024934bae17a2102b1 to your computer and use it in GitHub Desktop.

Select an option

Save mendes5/57c0794bb9e17b024934bae17a2102b1 to your computer and use it in GitHub Desktop.
simple timer to put on the game loop to avoid using setTimeout.
const tinyTimer = (function () {
const timers = []
function update() {
const time = performance.now()
let i = 0
while (i < timers.length) {
let timer = timers[i]
if (time > timer.start + timer.duration) {
timer.callback()
timers.splice(i, 1)
} else {
i++
}
}
}
return {
timers: timers,
update: update
}
})()
function wait(duration, callback) {
tinyTimer.timers.push({
duration: duration,
start: performance.now(),
callback: callback,
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment