-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstopwatch.js
More file actions
40 lines (29 loc) · 827 Bytes
/
Copy pathstopwatch.js
File metadata and controls
40 lines (29 loc) · 827 Bytes
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
function Stopwatch() {
let startTime, endTime, running, duration = 0
this.start = function () {
if (running)
throw new Error('Stopwatch has alrrady started.');
running = true;
startTime = new Date();
};
this.stop = function () {
if (!running)
throw new Error('Stop watch has already stopped.');
running = false;
endTime = new Date();
const seconds = (endTime.getTime() - startTime.getTime()) / 1000;
duration = + seconds;
};
this.reset = function () {
startTime = null;
endTime = null;
running = false;
duration = 0;
};
Object.defineProperty(this, 'duration', {
get: function () {
return duration;
}
});
}
const sw = new Stopwatch()