Skip to content

Commit

Permalink
Fix for high resolution timer when tab is hidden - shim performance.n…
Browse files Browse the repository at this point in the history
…ow() if it doesn't exist. Reference #725
  • Loading branch information
Rycochet committed Aug 2, 2020
1 parent 9074630 commit b766f53
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
18 changes: 16 additions & 2 deletions packages/velocity/velocity.js
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,19 @@
};
})();

var performance = (function() {
var perf = window.performance || {};

if (!perf.hasOwnProperty("now")) {
var nowOffset = perf.timing && perf.timing.domComplete ? perf.timing.domComplete : (new Date()).getTime();

perf.now = function() {
return (new Date()).getTime() - nowOffset;
};
}
return perf;
})();

/* Array compacting. Copyright Lo-Dash. MIT License: https://github.com/lodash/lodash/blob/master/LICENSE.txt */
function compactSparseArray(array) {
var index = -1,
Expand Down Expand Up @@ -3601,8 +3614,9 @@
by the same Velocity call -- are properly batched into the same initial RAF tick and consequently remain in sync thereafter. */
if (timestamp) {
/* We normally use RAF's high resolution timestamp but as it can be significantly offset when the browser is
under high stress we give the option for choppiness over allowing the browser to drop huge chunks of frames. */
var timeCurrent = Velocity.timestamp && timestamp !== true ? timestamp : (new Date()).getTime();
under high stress we give the option for choppiness over allowing the browser to drop huge chunks of frames.
We use performance.now() and shim it if it doesn't exist for when the tab is hidden. */
var timeCurrent = Velocity.timestamp && timestamp !== true ? timestamp : performance.now();

/********************
Call Iteration
Expand Down
Loading

0 comments on commit b766f53

Please sign in to comment.