File tree 2 files changed +12
-10
lines changed
2 files changed +12
-10
lines changed Original file line number Diff line number Diff line change @@ -16,6 +16,7 @@ export function extractMilliseconds(time) {
16
16
}
17
17
18
18
export function format ( time ) {
19
+ time = Math . round ( time / 10 ) ;
19
20
const minutes = ( extractMinutes ( time ) + "" ) . padStart ( 2 , "0" ) ;
20
21
const seconds = ( extractSeconds ( time ) + "" ) . padStart ( 2 , "0" ) ;
21
22
const milliseconds = ( extractMilliseconds ( time ) + "" ) . padStart ( 2 , "0" ) ;
Original file line number Diff line number Diff line change @@ -2,33 +2,34 @@ import { format } from "./TimerFormatter.js";
2
2
3
3
class Stopwatch {
4
4
constructor ( ) {
5
- this . precision = 10 ;
6
-
7
5
this . timeElapsed = 0 ;
8
- this . interval = null ;
6
+ this . startTime = null ;
9
7
10
8
this . laps = [ ] ;
11
9
}
12
10
13
11
get isCounting ( ) {
14
- return ! ! this . interval ;
12
+ return ! ! this . startTime ;
15
13
}
16
14
17
15
get elapsed ( ) {
16
+ if ( this . isCounting ) {
17
+ this . timeElapsed += Date . now ( ) - this . startTime ;
18
+ this . startTime = Date . now ( ) ;
19
+ }
20
+
18
21
return format ( this . timeElapsed ) ;
19
22
}
20
23
21
24
start ( ) {
22
- if ( ! this . interval ) {
23
- this . interval = setInterval ( ( ) => {
24
- this . timeElapsed ++ ;
25
- } , this . precision ) ;
25
+ if ( ! this . isCounting ) {
26
+ this . startTime = Date . now ( ) ;
26
27
}
27
28
}
28
29
29
30
stop ( ) {
30
- clearInterval ( this . interval ) ;
31
- this . interval = null ;
31
+ this . timeElapsed += Date . now ( ) - this . startTime ;
32
+ this . startTime = null ;
32
33
}
33
34
34
35
reset ( ) {
You can’t perform that action at this time.
0 commit comments