forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime.js
More file actions
60 lines (55 loc) · 1.39 KB
/
time.js
File metadata and controls
60 lines (55 loc) · 1.39 KB
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
/**
* @private
* @function
* @name now
* @description Get current time in milliseconds. Use it to measure time difference. Reference time may differ on different platforms.
* @returns {number} The time in milliseconds.
*/
const now = (typeof window !== 'undefined') && window.performance && window.performance.now && window.performance.timing ? function () {
return window.performance.now();
} : Date.now;
/**
* @private
* @class
* @name Timer
* @description Create a new Timer instance.
* @classdesc A Timer counts milliseconds from when start() is called until when stop() is called.
*/
class Timer {
constructor() {
this._isRunning = false;
this._a = 0;
this._b = 0;
}
/**
* @private
* @function
* @name Timer#start
* @description Start the timer.
*/
start() {
this._isRunning = true;
this._a = now();
}
/**
* @private
* @function
* @name Timer#stop
* @description Stop the timer.
*/
stop() {
this._isRunning = false;
this._b = now();
}
/**
* @private
* @function
* @name Timer#getMilliseconds
* @description Get the number of milliseconds that passed between start() and stop() being called.
* @returns {number} The elapsed milliseconds.
*/
getMilliseconds() {
return this._b - this._a;
}
}
export { now, Timer };