Created
April 28, 2021 21:49
-
-
Save weslleyaraujo/ef9adcd47b9633f4b5508c8078beb8d2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function useTimeout(callback: () => void, delay = 1000 * 5) { | |
const callbackRef = useRef<typeof callback | null>(null); | |
useEffect(() => { | |
callbackRef.current = callback; | |
}, [callback]); | |
useEffect(() => { | |
function execute() { | |
callbackRef.current?.(); | |
} | |
if (delay) { | |
const id = setTimeout(execute, delay); | |
return () => clearTimeout(id); | |
} | |
}, [callback, delay]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment