Skip to content

Instantly share code, notes, and snippets.

@ilhamgusti
Created October 29, 2019 05:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ilhamgusti/bce1575f332caa635e0417c1946ee316 to your computer and use it in GitHub Desktop.
Save ilhamgusti/bce1575f332caa635e0417c1946ee316 to your computer and use it in GitHub Desktop.
function useInterval(callback) {
const savedCallback = useRef();
useEffect(() => {
savedCallback.current = callback;
});
useEffect(() => {
function tick() {
savedCallback.current();
}
let id = setInterval(tick, 1000);
return () => clearInterval(id);
}, []);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment