@@ -18,20 +18,28 @@ export default function useNoticeTimer(
1818 const onEventUpdate = useEvent ( onUpdate ) ;
1919
2020 const [ walking , setWalking ] = React . useState ( durationMs > 0 ) ;
21- const startTimestampRef = React . useRef < number | null > ( null ) ;
2221 const passTimeRef = React . useRef ( 0 ) ;
22+ const lastRafTimeRef = React . useRef < number | null > ( null ) ;
2323
2424 function syncPassTime ( ) {
2525 const now = Date . now ( ) ;
26- const passedTime = now - ( startTimestampRef . current || now ) ;
27- startTimestampRef . current = now ;
28- passTimeRef . current += passedTime ;
26+ const lastRafTime = lastRafTimeRef . current ;
27+
28+ if ( lastRafTime !== null ) {
29+ passTimeRef . current += now - lastRafTime ;
30+ }
31+
32+ lastRafTimeRef . current = now ;
2933 }
3034
3135 const onPause = React . useCallback ( ( ) => {
3236 syncPassTime ( ) ;
37+ if ( durationMs > 0 ) {
38+ onEventUpdate ( Math . min ( passTimeRef . current / durationMs , 1 ) ) ;
39+ }
40+ lastRafTimeRef . current = null ;
3341 setWalking ( false ) ;
34- } , [ ] ) ;
42+ } , [ durationMs , onEventUpdate ] ) ;
3543
3644 const onResume = React . useCallback ( ( ) => {
3745 if ( durationMs > 0 ) {
@@ -43,16 +51,15 @@ export default function useNoticeTimer(
4351
4452 React . useEffect ( ( ) => {
4553 if ( durationMs <= 0 ) {
46- startTimestampRef . current = null ;
54+ lastRafTimeRef . current = null ;
4755 onEventUpdate ( 0 ) ;
4856 return ;
4957 }
5058
51- syncPassTime ( ) ;
5259 onEventUpdate ( Math . min ( passTimeRef . current / durationMs , 1 ) ) ;
5360
5461 if ( ! walking ) {
55- startTimestampRef . current = null ;
62+ lastRafTimeRef . current = null ;
5663 return ;
5764 }
5865
@@ -62,6 +69,8 @@ export default function useNoticeTimer(
6269 return ;
6370 }
6471
72+ lastRafTimeRef . current = Date . now ( ) ;
73+
6574 const timeout = window . setTimeout ( ( ) => {
6675 passTimeRef . current = durationMs ;
6776 onEventUpdate ( 1 ) ;
@@ -85,14 +94,13 @@ export default function useNoticeTimer(
8594 }
8695 }
8796
88- startTimestampRef . current = Date . now ( ) ;
8997 rafId = raf ( step ) ;
9098
9199 return ( ) => {
92100 window . clearTimeout ( timeout ) ;
93101 raf . cancel ( rafId ) ;
94102 } ;
95- } , [ durationMs , walking ] ) ;
103+ } , [ durationMs , onEventClose , onEventUpdate , trackProgress , walking ] ) ;
96104
97105 return [ onResume , onPause ] as const ;
98106}
0 commit comments