Skip to content

Commit e08b1fe

Browse files
committed
Use Unix timestamp instead of incremented int
1 parent a4a8618 commit e08b1fe

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

lib/TimerFormatter.js

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export function extractMilliseconds(time) {
1616
}
1717

1818
export function format(time) {
19+
time = Math.round(time / 10);
1920
const minutes = (extractMinutes(time) + "").padStart(2, "0");
2021
const seconds = (extractSeconds(time) + "").padStart(2, "0");
2122
const milliseconds = (extractMilliseconds(time) + "").padStart(2, "0");

lib/stopwatch.js

+11-10
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,34 @@ import { format } from "./TimerFormatter.js";
22

33
class Stopwatch {
44
constructor() {
5-
this.precision = 10;
6-
75
this.timeElapsed = 0;
8-
this.interval = null;
6+
this.startTime = null;
97

108
this.laps = [];
119
}
1210

1311
get isCounting() {
14-
return !!this.interval;
12+
return !!this.startTime;
1513
}
1614

1715
get elapsed() {
16+
if (this.isCounting) {
17+
this.timeElapsed += Date.now() - this.startTime;
18+
this.startTime = Date.now();
19+
}
20+
1821
return format(this.timeElapsed);
1922
}
2023

2124
start() {
22-
if (!this.interval) {
23-
this.interval = setInterval(() => {
24-
this.timeElapsed++;
25-
}, this.precision);
25+
if (!this.isCounting) {
26+
this.startTime = Date.now();
2627
}
2728
}
2829

2930
stop() {
30-
clearInterval(this.interval);
31-
this.interval = null;
31+
this.timeElapsed += Date.now() - this.startTime;
32+
this.startTime = null;
3233
}
3334

3435
reset() {

0 commit comments

Comments
 (0)