@@ -6,7 +6,7 @@ import { useCallback, useEffect, useRef } from 'react';
66 */
77export const useInactivityTimer = ( callback : ( ) => void , delay : number ) => {
88 const savedCallback = useRef < ( ( ) => void ) | null > ( null ) ;
9- const timeoutRef = useRef < NodeJS . Timeout | null > ( null ) ;
9+ const timeoutRef = useRef < ReturnType < typeof setTimeout > | null > ( null ) ;
1010
1111 // Remember the latest callback
1212 useEffect ( ( ) => {
@@ -18,10 +18,12 @@ export const useInactivityTimer = (callback: () => void, delay: number) => {
1818 if ( timeoutRef . current ) {
1919 clearTimeout ( timeoutRef . current ) ;
2020 }
21-
2221 if ( delay !== null && savedCallback . current ) {
2322 timeoutRef . current = setTimeout ( ( ) => {
23+ // Fire callback once inactivity threshold reached
2424 savedCallback . current ?.( ) ;
25+ // Schedule next run while still inactive
26+ resetTimer ( ) ;
2527 } , delay ) ;
2628 }
2729 } , [ delay ] ) ;
@@ -31,8 +33,6 @@ export const useInactivityTimer = (callback: () => void, delay: number) => {
3133 if ( delay === null ) {
3234 return ;
3335 }
34-
35- // Events that indicate user activity
3636 const events = [
3737 'mousedown' ,
3838 'mousemove' ,
@@ -47,7 +47,7 @@ export const useInactivityTimer = (callback: () => void, delay: number) => {
4747 document . addEventListener ( event , resetTimer , { passive : true } ) ;
4848 } ) ;
4949
50- // Start the initial timer
50+ // Start initial timer
5151 resetTimer ( ) ;
5252
5353 // Cleanup function
0 commit comments