import { useRef , useEffect } from 'react' ; type CallbackFunction = ( ) => void ; interface CallbackRef { ( ) : void ; } export function useInterval ( callback , delay ) { const savedCallback = useRef < CallbackRef > ( ) ; useEffect ( ( ) => { savedCallback . current = callback ; } , [ callback ] ) ; useEffect ( ( ) => { if ( delay > 0 ) { const id = setInterval ( tick , delay ) ; return ( ) => clearInterval ( id ) ; } } , [ delay ] ) ; function tick ( ) { if ( savedCallback . current ) { savedCallback . current ( ) ; } } }