File tree 2 files changed +21
-19
lines changed
2 files changed +21
-19
lines changed Original file line number Diff line number Diff line change @@ -15,44 +15,44 @@ export interface TimerRefAttributes {
15
15
16
16
export const Timer = forwardRef < TimerRefAttributes > ( ( _ , ref ) : JSX . Element => {
17
17
const { ctx } = useBoardContext ( ) ;
18
- const [ time , setTime ] = useState ( 0 ) ;
18
+ const [ start , setStart ] = useState ( 0 ) ;
19
+ const [ now , setNow ] = useState ( 0 ) ;
19
20
const intervalRef = useRef < NodeJS . Timer | null > ( null ) ;
20
21
21
22
useImperativeHandle (
22
23
ref ,
23
24
( ) => ( {
24
25
start : ( ) => {
25
26
if ( ! intervalRef . current ) {
27
+ setStart ( Date . now ( ) ) ;
28
+ setNow ( Date . now ( ) ) ;
26
29
const intervalID = setInterval ( ( ) => {
27
- setTime ( ( prev ) => prev + 1 ) ;
28
- } , 10 ) ;
30
+ setNow ( Date . now ( ) ) ;
31
+ } , 100 ) ;
29
32
intervalRef . current = intervalID ;
30
33
}
31
34
} ,
32
35
33
36
isRunning : ( ) => intervalRef . current !== null ,
34
37
35
38
getTime : ( ) => {
36
- return time ;
39
+ return Math . floor ( ( now - start ) / 10 ) ;
37
40
} ,
38
41
} ) ,
39
- [ time ]
42
+ [ now , start ]
40
43
) ;
41
44
42
45
useEffect ( ( ) => {
43
46
if ( ctx . gameover && intervalRef . current ) {
44
47
clearInterval ( intervalRef . current ) ;
48
+ setNow ( Date . now ( ) ) ;
45
49
intervalRef . current = null ;
46
50
}
47
51
} , [ ctx . gameover , intervalRef ] ) ;
48
52
49
- return (
50
- < h1 id = "timer" >
51
- { Math . floor ( time / 100 )
52
- . toString ( )
53
- . padStart ( 3 , "0" ) }
54
- </ h1 >
55
- ) ;
53
+ const time = Math . floor ( ( now - start ) / 1000 ) ;
54
+
55
+ return < h1 id = "timer" > { time . toString ( ) . padStart ( 3 , "0" ) } </ h1 > ;
56
56
} ) ;
57
57
58
58
Timer . displayName = "Timer" ;
Original file line number Diff line number Diff line change @@ -30,7 +30,9 @@ describe("Timer tests", () => {
30
30
</ BoardContext . Provider >
31
31
) ;
32
32
33
- timerRef . current . start ( ) ;
33
+ act ( ( ) => {
34
+ timerRef . current . start ( ) ;
35
+ } ) ;
34
36
35
37
expect ( timerRef . current . isRunning ( ) ) . toBe ( true ) ;
36
38
} ) ;
@@ -44,9 +46,11 @@ describe("Timer tests", () => {
44
46
</ BoardContext . Provider >
45
47
) ;
46
48
47
- timerRef . current . start ( ) ;
48
- boardContext . ctx . gameover = { isWin : true } ;
49
+ act ( ( ) => {
50
+ timerRef . current . start ( ) ;
51
+ } ) ;
49
52
53
+ boardContext . ctx . gameover = { isWin : true } ;
50
54
rerender (
51
55
< BoardContext . Provider value = { boardContext } >
52
56
< Timer ref = { timerRef } />
@@ -65,9 +69,8 @@ describe("Timer tests", () => {
65
69
</ BoardContext . Provider >
66
70
) ;
67
71
68
- timerRef . current . start ( ) ;
69
-
70
72
act ( ( ) => {
73
+ timerRef . current . start ( ) ;
71
74
vi . advanceTimersByTime ( 5000 ) ;
72
75
} ) ;
73
76
@@ -85,9 +88,8 @@ describe("Timer tests", () => {
85
88
</ BoardContext . Provider >
86
89
) ;
87
90
88
- timerRef . current . start ( ) ;
89
-
90
91
act ( ( ) => {
92
+ timerRef . current . start ( ) ;
91
93
vi . advanceTimersByTime ( 5000 ) ;
92
94
} ) ;
93
95
You can’t perform that action at this time.
0 commit comments