-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Expand file tree
/
Copy pathchronometer.js
More file actions
85 lines (67 loc) · 1.65 KB
/
chronometer.js
File metadata and controls
85 lines (67 loc) · 1.65 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
class Chronometer
{
constructor()
{
this.currentTime = 0;
this.intervalId = null;
}
start(callback)
{
this.intervalId = setInterval(() => {
this.currentTime++;
if (callback) callback();
} , 1000);
}
getMinutes()
{
let minutes = 0;
if (typeof this.currentTime !== 'number' || isNaN(this.currentTime)) {
throw new Error('Invalid currentTime value');
}
minutes = Math.floor(this.currentTime / 60);
return (minutes);
}
getSeconds()
{
let seconds = 0;
if (typeof this.currentTime !== 'number' || isNaN(this.currentTime)) {
throw new Error('Invalid currentTime value');
}
seconds = Math.floor(this.currentTime % 60);
return (seconds);
}
// getMiliSeconds()
// {
// let miliSeconds = 0;
// if (typeof this.currentTime !== 'number' || isNaN(this.currentTime)) {
// throw new Error('Invalid currentTime value');
// }
// miliSeconds = Math.floor(this.currentTime % 100);
// return (miliSeconds);
// }
computeTwoDigitNumber(value)
{
if (typeof value !== 'number')
throw new Error('Invalid type of value');
return (('0' + value).slice(-2));
}
stop()
{
clearInterval(this.intervalId);
}
reset()
{
this.currentTime = 0;
}
split()
{
const minutes = this.computeTwoDigitNumber(this.getMinutes());
const seconds = this.computeTwoDigitNumber(this.getSeconds());
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;
}