Use Date.now() if available since it's faster

This commit is contained in:
Pat Cavit 2016-07-29 23:18:11 -07:00
parent 40b2053895
commit 47b9aba680

View file

@ -1,12 +1,16 @@
"use strict"
var ts = Date.now || function() {
return new Date().getTime()
}
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()
var now = ts()
if (synchronous === true || last === 0 || now - last >= time) {
last = now
callback()
@ -15,7 +19,7 @@ module.exports = function(callback) {
pending = timeout(function() {
pending = null
callback()
last = new Date().getTime()
last = ts()
}, time - (now - last))
}
}