Something went wrong. Try again.
Bluesky app fork with some witchin' additions 💫 forked from jollywhoppers.com/witchsky.app
Something went wrong. Try again.
795 B · 32 lines
TypeScript
at main
123456789101112131415161718192021222324252627282930313233import * as React from 'react'
/** * Helper hook to run persistent timers on views */export function useTimer(time: number, handler: () => void) { const timer = React.useRef<undefined | NodeJS.Timeout>(undefined)
// function to restart the timer const reset = React.useCallback(() => { if (timer.current) { clearTimeout(timer.current) } timer.current = setTimeout(handler, time) }, [time, timer, handler])
// function to cancel the timer const cancel = React.useCallback(() => { if (timer.current) { clearTimeout(timer.current) timer.current = undefined } }, [timer])
// start the timer immediately React.useEffect(() => { reset() // eslint-disable-next-line react-hooks/exhaustive-deps }, [])
return [reset, cancel]}