-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchronometer.js
More file actions
53 lines (44 loc) · 1.2 KB
/
chronometer.js
File metadata and controls
53 lines (44 loc) · 1.2 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
export class Chronometer {
constructor(limit) {
this.startTime = limit
this.currentTime = limit;
this.intervalId = null;
}
start(callback) {
this.intervalId = setInterval(() => {
this.currentTime--;
if (callback && typeof callback === 'function') {
callback();
};
}, 1000);
}
getMinutes() {
if (this.currentTime === 0) { return "00" }
return this.computeTwoDigitNumber(Math.floor(this.currentTime / 60));
}
getSeconds() {
return this.computeTwoDigitNumber(this.currentTime % 60);
}
computeTwoDigitNumber(value) {
let stringValue = value.toString()
if (stringValue.length === 2) return stringValue;
else return "0" + stringValue;
}
stop() {
clearInterval(this.intervalId);
this.currentTime = this.startTime
}
reset() {
return this.currentTime = 0;
}
split() {
let seconds = this.computeTwoDigitNumber(this.getSeconds());
let minutes = this.computeTwoDigitNumber(this.getMinutes());
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;
}