import { useEffect , useState } from 'react' ; const useTimeout = ( onTimeoutStop , duration ) => { const [ timerId , setTimerId ] = useState ( null ) ; const removeTimeout = ( ) => { if ( timerId ) { window . clearTimeout ( timerId ) ; setTimerId ( null ) ; } } ; const start = ( ) => { removeTimeout ( ) ; setTimerId ( window . setTimeout ( onStop , duration ) ) ; } ; const onStop = ( ) => { removeTimeout ( ) ; onTimeoutStop ( ) ; } ; useEffect ( ( ) => { return ( ) => { removeTimeout ( ) ; } ; } , [ duration , onTimeoutStop ] ) ; return { removeTimeout , startTimeout : start } ; } ; export { useTimeout } ;