-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.js
47 lines (41 loc) · 886 Bytes
/
timer.js
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
export default class Timer {
constructor () {
this._running = false
this._startTime = null
this._stopTime = null
}
start () {
if (this._running) {
return
}
this._startTime = new Date()
this._running = true
}
stop () {
if (!this._running) {
return
}
this._stopTime = new Date()
this._running = false
}
reset () {
this._running = false
this._startTime = null
this._stopTime = null
}
getElapsedTime () {
if (this._startTime === null) {
return ['0', '0']
}
let elapsed = 0
if (this._stopTime !== null) {
elapsed = this._stopTime - this._startTime
}
else {
elapsed = new Date() - this._startTime
}
let seconds = Math.floor(elapsed / 1000)
const minutes = Math.floor(seconds / 60)
return [minutes.toString(), (seconds % 60).toString()]
}
}