UNPKG

529 BJavaScriptView Raw
1class Stopwatch {
2 start() {
3 this.start = Date.now();
4 this.end = undefined;
5 }
6
7 stop() {
8 if (typeof this.start !== 'number') {
9 throw new TypeError('.start() has not been called');
10 }
11
12 if (typeof this.end === 'number') {
13 throw new TypeError('.stop() has already been called');
14 }
15
16 this.end = Date.now();
17 }
18
19 get duration() {
20 if (typeof this.end !== 'number') {
21 throw new TypeError('.stop() has not been called');
22 }
23
24 return Math.round((this.end - this.start) / 1000);
25 }
26}
27
28module.exports = Stopwatch;