cables_dev/cables/src/core/timer.js
import { EventTarget } from "./eventtarget.js";
/** @namespace CABLES */
export const internalNow = function ()
{
return window.performance.now();
};
/**
* current time in milliseconds
* @memberof CABLES
* @function now
* @static
*/
export const now = function ()
{
return internalNow();
};
// ----------------------------
/**
* Measuring time
* @namespace external:CABLES#Timer
* @hideconstructor
* @class
*/
const Timer = function ()
{
EventTarget.apply(this);
this._timeStart = internalNow();
this._timeOffset = 0;
this._currentTime = 0;
this._lastTime = 0;
this._paused = true;
this._delay = 0;
this.overwriteTime = -1;
};
Timer.prototype._internalNow = function ()
{
if (this._ts) return this._ts;
return internalNow();
};
Timer.prototype._getTime = function ()
{
this._lastTime = (this._internalNow() - this._timeStart) / 1000;
return this._lastTime + this._timeOffset;
};
Timer.prototype.setDelay = function (d)
{
this._delay = d;
this.emitEvent("timeChange");
};
/**
* @function
* @memberof Timer
* @instance
* @description returns true if timer is playing
* @return {Boolean} value
*/
Timer.prototype.isPlaying = function ()
{
return !this._paused;
};
/**
* @function
* @memberof Timer
* @instance
* @param ts
* @description update timer
* @return {Number} time
*/
Timer.prototype.update = function (ts)
{
if (ts) this._ts = ts;
if (this._paused) return;
this._currentTime = this._getTime();
return this._currentTime;
};
/**
* @function
* @memberof Timer
* @instance
* @return {Number} time in milliseconds
*/
Timer.prototype.getMillis = function ()
{
return this.get() * 1000;
};
/**
* @function
* @memberof Timer
* @instance
* @return {Number} value time in seconds
*/
Timer.prototype.get = Timer.prototype.getTime = function ()
{
if (this.overwriteTime >= 0) return this.overwriteTime - this._delay;
return this._currentTime - this._delay;
};
/**
* toggle between play/pause state
* @function
* @memberof Timer
* @instance
*/
Timer.prototype.togglePlay = function ()
{
if (this._paused) this.play();
else this.pause();
};
/**
* set current time
* @function
* @memberof Timer
* @instance
* @param {Number} t
*/
Timer.prototype.setTime = function (t)
{
if (isNaN(t) || t < 0) t = 0;
this._timeStart = this._internalNow();
this._timeOffset = t;
this._currentTime = t;
this.emitEvent("timeChange");
};
Timer.prototype.setOffset = function (val)
{
if (this._currentTime + val < 0)
{
this._timeStart = this._internalNow();
this._timeOffset = 0;
this._currentTime = 0;
}
else
{
this._timeOffset += val;
this._currentTime = this._lastTime + this._timeOffset;
}
this.emitEvent("timeChange");
};
/**
* (re)starts the timer
* @function
* @memberof Timer
* @instance
*/
Timer.prototype.play = function ()
{
this._timeStart = this._internalNow();
this._paused = false;
this.emitEvent("playPause");
};
/**
* pauses the timer
* @function
* @memberof Timer
* @instance
*/
Timer.prototype.pause = function ()
{
this._timeOffset = this._currentTime;
this._paused = true;
this.emitEvent("playPause");
};
export { Timer };