- pubsub is a basic pubsub impl - autoredraw is glue code to register callback to pubsub and onevent moved e.redraw to autoredraw
22 lines
595 B
JavaScript
22 lines
595 B
JavaScript
"use strict"
|
|
|
|
module.exports = function(callback) {
|
|
//60fps translates to 16.6ms, round it down since setTimeout requires int
|
|
var time = 16
|
|
var last = 0, pending = null
|
|
var timeout = typeof requestAnimationFrame === "function" ? requestAnimationFrame : setTimeout
|
|
return function(synchronous) {
|
|
var now = new Date().getTime()
|
|
if (synchronous === true || last === 0 || now - last >= time) {
|
|
last = now
|
|
callback()
|
|
}
|
|
else if (pending === null) {
|
|
pending = timeout(function() {
|
|
pending = 0
|
|
callback()
|
|
last = new Date().getTime()
|
|
}, time - (now - last))
|
|
}
|
|
}
|
|
}
|