Created
January 21, 2016 15:00
-
-
Save majido/6868fbfd16bbe880e9e0 to your computer and use it in GitHub Desktop.
Translating DOMTimeStamp to DOMHighResTimeStamp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Translate Event.timeStamp to a DOMHighResTimeStamp that can be compare with | |
// performance.now(). this becomes a no-op for browsers that provide | |
// high resolution event timestamp. | |
toHighResTimeStamp = (function(testTimeStamp){ | |
function timeNear(a, b) { | |
var d = 1000 * 60 * 5; | |
return a > b - d && a < b + d; | |
} | |
var timebaseDelta_ = 0; | |
// Detect if this browser is using DOMTimeStamp and adjust timebase if needed. | |
if (timeNear(testTimeStamp, Date.now())) { | |
// Delta is the difference between current time is both timebases. | |
timebaseDelta_ = Math.round(performance.now() - Date.now()); | |
} | |
return function(timeStamp) { | |
return timeStamp + timebaseDelta_; | |
} | |
}(new Event('test').timeStamp)); | |
// Usage example: | |
// var latency = performance.now() - toHighResTimeStamp(e.timeStamp); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment