import { useEffect , useRef , useCallback } from 'react' ; import { useEventCallback } from '@ringcentral/juno' ; example export const useInterval = ( /** callback with times, times start from 1 */ callback , /** what delay time of that interval */ delay = 0 , /** is that start interval immediately */ startImmediately = true ) => { const intervalRef = useRef < number > ( ) ; const countRef = useRef ( 0 ) ; const getRunning = useEventCallback ( ( ) => ! ! intervalRef . current ) ; const savedCallback = useEventCallback ( ( times ) => { callback ( times ) ; } ) ; const pause = useCallback ( ( ) => { if ( intervalRef . current ) { clearInterval ( intervalRef . current ) ; intervalRef . current = undefined ; } } , [ ] ) ; const cancel = useCallback ( ( ) => { pause ( ) ; countRef . current = 0 ; } , [ pause ] ) ; const play = useCallback ( ( ) => { const isRunning = getRunning ( ) ; if ( isRunning ) return ; intervalRef . current = window . setInterval ( ( ) => { countRef . current ++ ; savedCallback ( countRef . current ) ; } , delay ) ; } , [ delay , getRunning , savedCallback ] ) ; useEffect ( ( ) => { if ( startImmediately ) { play ( ) ; } return ( ) => cancel ( ) ; } , [ cancel , play , startImmediately ] ) ; return { /**
         * cancel current interval
         */ cancel , /**
         * pause current interval
         */ pause , /**
         * play current interval
         */ play , /**
         * return current interval running state
         */ getRunning , } ; } ;