Skip to content

Instantly share code, notes, and snippets.

@majido
Created January 21, 2016 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save majido/6868fbfd16bbe880e9e0 to your computer and use it in GitHub Desktop.
Save majido/6868fbfd16bbe880e9e0 to your computer and use it in GitHub Desktop.
Translating DOMTimeStamp to DOMHighResTimeStamp
// 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