Last active
April 3, 2017 04:04
-
-
Save mendes5/57c0794bb9e17b024934bae17a2102b1 to your computer and use it in GitHub Desktop.
simple timer to put on the game loop to avoid using setTimeout.
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 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