Home Reference Source

cables_dev/cables/src/core/timer.js

  1. import { EventTarget } from "./eventtarget.js";
  2.  
  3. /** @namespace CABLES */
  4.  
  5. export const internalNow = function ()
  6. {
  7. return window.performance.now();
  8. };
  9.  
  10.  
  11.  
  12. /**
  13. * current time in milliseconds
  14. * @memberof CABLES
  15. * @function now
  16. * @static
  17. */
  18. export const now = function ()
  19. {
  20. return internalNow();
  21. };
  22.  
  23. // ----------------------------
  24.  
  25. /**
  26. * Measuring time
  27. * @namespace external:CABLES#Timer
  28. * @hideconstructor
  29. * @class
  30. */
  31. const Timer = function ()
  32. {
  33. EventTarget.apply(this);
  34.  
  35. this._timeStart = internalNow();
  36. this._timeOffset = 0;
  37.  
  38. this._currentTime = 0;
  39. this._lastTime = 0;
  40. this._paused = true;
  41. this._delay = 0;
  42. this.overwriteTime = -1;
  43. };
  44.  
  45.  
  46. Timer.prototype._internalNow = function ()
  47. {
  48. if (this._ts) return this._ts;
  49. return internalNow();
  50. };
  51.  
  52. Timer.prototype._getTime = function ()
  53. {
  54. this._lastTime = (this._internalNow() - this._timeStart) / 1000;
  55. return this._lastTime + this._timeOffset;
  56. };
  57.  
  58. Timer.prototype.setDelay = function (d)
  59. {
  60. this._delay = d;
  61. this.emitEvent("timeChange");
  62. };
  63.  
  64. /**
  65. * @function
  66. * @memberof Timer
  67. * @instance
  68. * @description returns true if timer is playing
  69. * @return {Boolean} value
  70. */
  71. Timer.prototype.isPlaying = function ()
  72. {
  73. return !this._paused;
  74. };
  75.  
  76. /**
  77. * @function
  78. * @memberof Timer
  79. * @instance
  80. * @param ts
  81. * @description update timer
  82. * @return {Number} time
  83. */
  84. Timer.prototype.update = function (ts)
  85. {
  86. if (ts) this._ts = ts;
  87. if (this._paused) return;
  88. this._currentTime = this._getTime();
  89.  
  90. return this._currentTime;
  91. };
  92.  
  93. /**
  94. * @function
  95. * @memberof Timer
  96. * @instance
  97. * @return {Number} time in milliseconds
  98. */
  99. Timer.prototype.getMillis = function ()
  100. {
  101. return this.get() * 1000;
  102. };
  103.  
  104. /**
  105. * @function
  106. * @memberof Timer
  107. * @instance
  108. * @return {Number} value time in seconds
  109. */
  110. Timer.prototype.get = Timer.prototype.getTime = function ()
  111. {
  112. if (this.overwriteTime >= 0) return this.overwriteTime - this._delay;
  113. return this._currentTime - this._delay;
  114. };
  115.  
  116. /**
  117. * toggle between play/pause state
  118. * @function
  119. * @memberof Timer
  120. * @instance
  121. */
  122. Timer.prototype.togglePlay = function ()
  123. {
  124. if (this._paused) this.play();
  125. else this.pause();
  126. };
  127.  
  128. /**
  129. * set current time
  130. * @function
  131. * @memberof Timer
  132. * @instance
  133. * @param {Number} t
  134. */
  135. Timer.prototype.setTime = function (t)
  136. {
  137. if (isNaN(t) || t < 0) t = 0;
  138. this._timeStart = this._internalNow();
  139. this._timeOffset = t;
  140. this._currentTime = t;
  141. this.emitEvent("timeChange");
  142. };
  143.  
  144. Timer.prototype.setOffset = function (val)
  145. {
  146. if (this._currentTime + val < 0)
  147. {
  148. this._timeStart = this._internalNow();
  149. this._timeOffset = 0;
  150. this._currentTime = 0;
  151. }
  152. else
  153. {
  154. this._timeOffset += val;
  155. this._currentTime = this._lastTime + this._timeOffset;
  156. }
  157. this.emitEvent("timeChange");
  158. };
  159.  
  160. /**
  161. * (re)starts the timer
  162. * @function
  163. * @memberof Timer
  164. * @instance
  165. */
  166. Timer.prototype.play = function ()
  167. {
  168. this._timeStart = this._internalNow();
  169. this._paused = false;
  170. this.emitEvent("playPause");
  171. };
  172.  
  173. /**
  174. * pauses the timer
  175. * @function
  176. * @memberof Timer
  177. * @instance
  178. */
  179. Timer.prototype.pause = function ()
  180. {
  181. this._timeOffset = this._currentTime;
  182. this._paused = true;
  183. this.emitEvent("playPause");
  184. };
  185.  
  186. export { Timer };