Skip to content

Instantly share code, notes, and snippets.

@alexeyraspopov
Last active September 20, 2019 20:02
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save alexeyraspopov/1233af30f77e553fc7c949acf5f61dad to your computer and use it in GitHub Desktop.
function useInterval(callback, delay) {
let handler = useRef(callback);
useEffect(() => {
handler.current = callback;
}, [callback]);
useEffect(() => {
let timer = setInterval(() => handler.current(), delay);
return () => clearInterval(timer);
}, [delay]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment