Skip to content

Commit

Permalink
Added support for high-resolution timers
Browse files Browse the repository at this point in the history
Has fallback to MS resolution timer on non-supported browsers
  • Loading branch information
Brandon Jones committed May 1, 2012
1 parent f189149 commit cd42d3b
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions game-shim.js
@@ -1,7 +1,7 @@
/**
* @fileoverview game-shim - Shims to normalize gaming-related APIs to their respective specs
* @author Brandon Jones
* @version 0.6
* @version 0.7
*/

/*
Expand Down Expand Up @@ -37,7 +37,8 @@
supports: {
fullscreen: true,
pointerLock: true,
gamepad: true
gamepad: true,
highResTimer: true
}
};

Expand Down Expand Up @@ -353,5 +354,35 @@
get: getter
});
}

//=======================
// High Resolution Timer
//=======================

if(!window.performance) {
window.performance = {};
}

if(!window.performance.timing) {
window.performance.timing = {
navigationStart: Date.now() // Terrible approximation, I know. Sorry.
};
}

if(!window.performance.now) {
window.performance.now = (function() {
// FYI: Mozilla supports high-res timers without prefixes.

if(window.performance.webkitNow) {
return window.performance.webkitNow;
}

GameShim.supports.highResTimer = false;

return function(){ // not supported, return a low-resolution approximation
return Date.now() - window.performance.timing.navigationStart;
};
})();
}

})((typeof(exports) != 'undefined') ? global : window); // Account for CommonJS environments

0 comments on commit cd42d3b

Please sign in to comment.