Skip to content

Instantly share code, notes, and snippets.

@WillSquire
Last active August 29, 2015 14:23
Show Gist options
  • Save WillSquire/002beb25bd587ed0395d to your computer and use it in GitHub Desktop.
Save WillSquire/002beb25bd587ed0395d to your computer and use it in GitHub Desktop.
Time - A singleton for getting current time and delta time.
/**
* 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