Files
JiboOs/docs/utils/Timer.js
2026-03-16 13:53:01 +02:00

105 lines
3.3 KiB
JavaScript

/**
* @description
* Handle the update loop and timing events.
* ```
* let jibo = require('jibo');
* jibo.timer.on('update', (elapsed) => {
* // do update
* });
* ```
* @namespace jibo.timer
*/
/**
* Fire when timer pause state changes.
* @event jibo.timer#pause
* @param {boolean} pause `true` if currently paused, `false` if resumed from stop.
*/
/**
* Fire when sound is resumed.
* @event jibo.timer#resumed
*/
/**
* Fire when sound is paused.
* @event jibo.timer#paused
*/
/**
* Fire every `requestAnimationFrame`. Should be considered
* the main update "loop." All skills should use this for frame updates.
* @event jibo.timer#update
* @param {int} elapse The time in milliseconds since the last frame update.
*/
/**
* Start the update loop.
* @method jibo.timer#start
*/
/**
* Stop the update loop.
* @method jibo.timer#stop
*/
/**
* Update loop callback.
* @method jibo.timer#update
* @private
*/
/**
* Pause loop callback.
* @name jibo.timer#paused
* @type {Boolean}
* @readOnly
*/
/**
* Works just like `window.setTimeout` but respects the pause.
* State of `jibo.timer`.
* @method jibo.timer#setTimeout
* @param {Function} callback Pass one argument, which is the `DelayedCall` instance.
* @param {int} delay The time in milliseconds or the number of frames (useFrames must be true).
* @param {Boolean} [useFrames=false] If the delay is frames (`true`) or milliseconds (`false`).
* @param {Boolean} [autoDestroy=true] If the DelayedCall object should be destroyed after completing.
* @return {jibo.timer.DelayedCall} The object for pausing, restarting, destroying etc.
*/
/**
* Drop-in replacement for `window.clearTimeout`. Works with `jibo.timer.setTimeout`
* @method jibo.timer#clearTimeout
* @param {jibo.timer.DelayedCall} call The call to remove.
*/
/**
* Works just like `window.setInterval` but respects the pause.
* State of jibo.timer.
* @method jibo.timer#setInterval
* @param {Function} callback Pass one argument, which is the DelayedCall instance.
* @param {int} delay The time in milliseconds or the number of frames ( if useFrames is `true`).
* @param {Boolean} [useFrames=false] If the delay is frames (`true`) or milliseconds (`false`).
* @return {jibo.timer.DelayedCall} The object for pausing, restarting, destroying etc.
*/
/**
* Calls the provided callback function on the next timer frame.
* State of `jibo.timer`.
* @method jibo.timer#nextTick
* @param {Function} callback Pass one argument, which is the `DelayedCall` instance.
* @param {Boolean} [autoDestroy=true] If the `DelayedCall` object should be destroyed after completing.
* @return {jibo.timer.DelayedCall} The object for pausing, restarting, destroying etc.
*/
/**
* Drop-in replacement for `window.clearInterval`, works with `jibo.timer.setInterval`
* @method jibo.timer#clearInterval
* @param {jibo.timer.DelayedCall} call The call to remove.
*/
/**
* Destroy the timer object.
* @method jibo.timer#destroy
* @private
*/