Spaces:
Running
Running
File size: 3,872 Bytes
30c32c8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
/**
* @fileoverview
* A utility for accurately measuring time.
* To use:
* ---
* var timer = new Timer();
* timer.start();
* ... pass some time ...
* var timeDifference = timer.timeElapsed();
* ---
* Or, you can use the `time` and `relativeTime`
* to do some measurement yourself.
*/
class Timer {
constructor (nowObj = Timer.nowObj) {
/**
* Used to store the start time of a timer action.
* Updated when calling `timer.start`.
*/
this.startTime = 0;
/**
* Used to pass custom logic for determining the value for "now",
* which is sometimes useful for compatibility with Scratch 2
*/
this.nowObj = nowObj;
/**
* Detirmins if this timer is paused or not
*/
this._pausedTime = null;
}
/**
* Disable use of self.performance for now as it results in lower performance
* However, instancing it like below (caching the self.performance to a local variable) negates most of the issues.
* @type {boolean}
*/
static get USE_PERFORMANCE () {
return false;
}
/**
* Legacy object to allow for us to call now to get the old style date time (for backwards compatibility)
* @deprecated This is only called via the nowObj.now() if no other means is possible...
*/
static get legacyDateCode () {
return {
now: function () {
return new Date().getTime();
}
};
}
/**
* Use this object to route all time functions through single access points.
*/
static get nowObj () {
if (Timer.USE_PERFORMANCE && typeof self !== 'undefined' && self.performance && 'now' in self.performance) {
return self.performance;
} else if (Date.now) {
return Date;
}
return Timer.legacyDateCode;
}
/**
* Return the currently known absolute time, in ms precision.
* @returns {number} ms elapsed since 1 January 1970 00:00:00 UTC.
*/
time () {
return this.nowObj.now();
}
/**
* Returns a time accurate relative to other times produced by this function.
* If possible, will use sub-millisecond precision.
* If not, will use millisecond precision.
* Not guaranteed to produce the same absolute values per-system.
* @returns {number} ms-scale accurate time relative to other relative times.
*/
relativeTime () {
return this.nowObj.now();
}
/**
* Start a timer for measuring elapsed time,
* at the most accurate precision possible.
*/
start () {
this.startTime = this.nowObj.now();
}
/**
* pause the timer
*/
pause() {
if (this._pausedTime) return;
this._pausedTime = this.timeElapsed();
}
/**
* unpause the timer
*/
play() {
if (!this._pausedTime) return;
this.startTime = this.nowObj.now() - this._pausedTime;
this._pausedTime = null;
}
timeElapsed () {
if (this._pausedTime) return this._pausedTime;
const now = this.nowObj.now();
return now - this.startTime;
}
/**
* Call a handler function after a specified amount of time has elapsed.
* @param {function} handler - function to call after the timeout
* @param {number} timeout - number of milliseconds to delay before calling the handler
* @returns {number} - the ID of the new timeout
*/
setTimeout (handler, timeout) {
return global.setTimeout(handler, timeout);
}
/**
* Clear a timeout from the pending timeout pool.
* @param {number} timeoutId - the ID returned by `setTimeout()`
* @memberof Timer
*/
clearTimeout (timeoutId) {
global.clearTimeout(timeoutId);
}
}
module.exports = Timer;
|