Last active
August 29, 2015 14:23
-
-
Save WillSquire/002beb25bd587ed0395d to your computer and use it in GitHub Desktop.
Time - A singleton for getting current time and delta time.
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
/** | |
* Time, a singleton for getting current time and delta time. | |
* I.e. Time.time or Time.deltaTime | |
* | |
* @returns {*} | |
* @constructor | |
*/ | |
var Time = function() { | |
// Singleton is returned if avalible else it is made. | |
if (Time.prototype._singletonInstance) | |
return Time.prototype._singletonInstance; | |
Time.prototype._singletonInstance = this; | |
this.time = new Date().getTime(); // Init to avoid insane initial values | |
this.deltaTime = 0; | |
/** | |
* Updates time and deltaTime on each frame | |
* | |
* @private | |
*/ | |
Frames().add(function() { | |
var now = new Date().getTime(); | |
this.deltaTime = (now - this.time) * 0.001; // Get in milliseconds | |
this.time = now; | |
}.bind(this)); | |
return this; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment