Created
September 28, 2016 09:46
-
-
Save andris9/c4144b0eaecd013fb9fbba959d7331e7 to your computer and use it in GitHub Desktop.
tick on every 50ms
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
<script> | |
let myWorker = new Worker('worker.js'); | |
let prev = Date.now(); | |
myWorker.onmessage = function(e) { | |
let now = Date.now(); | |
console.log(now - prev); | |
prev = now; | |
} | |
</script> |
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 TICK_INTERVAL = 50; | |
const START_TIME = performance.now(); | |
let expecting = 0; | |
let ticker = () => { | |
while (true) { | |
let curtime = performance.now(); | |
if (curtime >= expecting) { | |
expecting = (Math.floor((curtime - START_TIME) / TICK_INTERVAL) + 1) * TICK_INTERVAL + START_TIME; | |
postMessage(1); | |
return setTimeout(ticker, TICK_INTERVAL / 8 * 7); | |
} | |
} | |
}; | |
ticker(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment