mithril-vndb/api/throttle.js
Pat Cavit 2ca8fa6e66 Quiet some ESLint errors (#1060)
Some via config, a few via code edits
2016-05-19 22:18:31 -07:00

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))
}
}
}