WillSquire
6/30/2015 - 3:47 PM

Time - A singleton for getting current time and delta time.

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;
 };