Use Date.now() if available since it's faster
This commit is contained in:
parent
40b2053895
commit
47b9aba680
1 changed files with 6 additions and 2 deletions
|
|
@ -1,12 +1,16 @@
|
||||||
"use strict"
|
"use strict"
|
||||||
|
|
||||||
|
var ts = Date.now || function() {
|
||||||
|
return new Date().getTime()
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = function(callback) {
|
module.exports = function(callback) {
|
||||||
//60fps translates to 16.6ms, round it down since setTimeout requires int
|
//60fps translates to 16.6ms, round it down since setTimeout requires int
|
||||||
var time = 16
|
var time = 16
|
||||||
var last = 0, pending = null
|
var last = 0, pending = null
|
||||||
var timeout = typeof requestAnimationFrame === "function" ? requestAnimationFrame : setTimeout
|
var timeout = typeof requestAnimationFrame === "function" ? requestAnimationFrame : setTimeout
|
||||||
return function(synchronous) {
|
return function(synchronous) {
|
||||||
var now = new Date().getTime()
|
var now = ts()
|
||||||
if (synchronous === true || last === 0 || now - last >= time) {
|
if (synchronous === true || last === 0 || now - last >= time) {
|
||||||
last = now
|
last = now
|
||||||
callback()
|
callback()
|
||||||
|
|
@ -15,7 +19,7 @@ module.exports = function(callback) {
|
||||||
pending = timeout(function() {
|
pending = timeout(function() {
|
||||||
pending = null
|
pending = null
|
||||||
callback()
|
callback()
|
||||||
last = new Date().getTime()
|
last = ts()
|
||||||
}, time - (now - last))
|
}, time - (now - last))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue