Buckets:
| /** | |
| * Class for keeping track of time. | |
| */ | |
| class Clock { | |
| /** | |
| * Constructs a new clock. | |
| * | |
| * @param {boolean} [autoStart=true] - Whether to automatically start the clock when | |
| * `getDelta()` is called for the first time. | |
| */ | |
| constructor( autoStart = true ) { | |
| /** | |
| * If set to `true`, the clock starts automatically when `getDelta()` is called | |
| * for the first time. | |
| * | |
| * @type {boolean} | |
| * @default true | |
| */ | |
| this.autoStart = autoStart; | |
| /** | |
| * Holds the time at which the clock's `start()` method was last called. | |
| * | |
| * @type {number} | |
| * @default 0 | |
| */ | |
| this.startTime = 0; | |
| /** | |
| * Holds the time at which the clock's `start()`, `getElapsedTime()` or | |
| * `getDelta()` methods were last called. | |
| * | |
| * @type {number} | |
| * @default 0 | |
| */ | |
| this.oldTime = 0; | |
| /** | |
| * Keeps track of the total time that the clock has been running. | |
| * | |
| * @type {number} | |
| * @default 0 | |
| */ | |
| this.elapsedTime = 0; | |
| /** | |
| * Whether the clock is running or not. | |
| * | |
| * @type {boolean} | |
| * @default true | |
| */ | |
| this.running = false; | |
| } | |
| /** | |
| * Starts the clock. When `autoStart` is set to `true`, the method is automatically | |
| * called by the class. | |
| */ | |
| start() { | |
| this.startTime = now(); | |
| this.oldTime = this.startTime; | |
| this.elapsedTime = 0; | |
| this.running = true; | |
| } | |
| /** | |
| * Stops the clock. | |
| */ | |
| stop() { | |
| this.getElapsedTime(); | |
| this.running = false; | |
| this.autoStart = false; | |
| } | |
| /** | |
| * Returns the elapsed time in seconds. | |
| * | |
| * @return {number} The elapsed time. | |
| */ | |
| getElapsedTime() { | |
| this.getDelta(); | |
| return this.elapsedTime; | |
| } | |
| /** | |
| * Returns the delta time in seconds. | |
| * | |
| * @return {number} The delta time. | |
| */ | |
| getDelta() { | |
| let diff = 0; | |
| if ( this.autoStart && ! this.running ) { | |
| this.start(); | |
| return 0; | |
| } | |
| if ( this.running ) { | |
| const newTime = now(); | |
| diff = ( newTime - this.oldTime ) / 1000; | |
| this.oldTime = newTime; | |
| this.elapsedTime += diff; | |
| } | |
| return diff; | |
| } | |
| } | |
| function now() { | |
| return performance.now(); | |
| } | |
| export { Clock }; | |
Xet Storage Details
- Size:
- 2.07 kB
- Xet hash:
- 50635747bdc00eebc087e1aad3c7f9f65e3f4074bfd44b8426ce1055f0e7cf73
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.