Created
July 27, 2021 07:46
-
-
Save Andrewnt219/e83f948247719bc84f5f2fd35110bafb 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 useInterval(callback, delay) { | |
const intervalRef = React.useRef(null); | |
const savedCallback = React.useRef(callback); | |
React.useEffect(() => { | |
savedCallback.current = callback; | |
}); | |
React.useEffect(() => { | |
const tick = () => savedCallback.current(); | |
if (typeof delay === 'number') { | |
intervalRef.current = window.setInterval(tick, delay); | |
return () => window.clearInterval(intervalRef.current); | |
} | |
}, [delay]); | |
return intervalRef; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment