From 47b9aba68050230eeff0754f4076b7596cbe9eb9 Mon Sep 17 00:00:00 2001 From: Pat Cavit Date: Fri, 29 Jul 2016 23:18:11 -0700 Subject: [PATCH] Use Date.now() if available since it's faster --- api/throttle.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/api/throttle.js b/api/throttle.js index cd4f4470..7cb4b2d7 100644 --- a/api/throttle.js +++ b/api/throttle.js @@ -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)) } }