-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathchronometer.js
49 lines (40 loc) · 1.07 KB
/
chronometer.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
48
49
class Chronometer {
constructor() {
this.currentTime = 0;
this.intervalId = null;
}
start(callback) {
this.intervalId = setInterval(() => {
this.currentTime++; // Increment by 1 each second
}, 1000);
}
getMinutes() {
return Math.floor(this.currentTime / 60);
}
getSeconds() {
return this.currentTime % 60;
}
getMilliseconds() {
return Math.floor((this.currentTime % 1000) / 10); // Convert to two-digit milliseconds
}
computeTwoDigitNumber(value) {
return value.toString().padStart(2, '0');
}
stop() {
clearInterval(this.intervalId);
}
reset() {
this.currentTime = 0;
}
split() {
const minutes = this.computeTwoDigitNumber(this.getMinutes());
const seconds = this.computeTwoDigitNumber(this.getSeconds());
const milliseconds = this.computeTwoDigitNumber(this.getMilliseconds());
return `${minutes}:${seconds}`;
}
}
// The following is required to make unit tests work.
/* Environment setup. Do not modify the below code. */
if (typeof module !== 'undefined') {
module.exports = Chronometer;
}