Skip to content

Instantly share code, notes, and snippets.

@ohansemmanuel
Created April 13, 2019 09:25
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 ohansemmanuel/f384456a7589816254c8b8297dad26be to your computer and use it in GitHub Desktop.
Save ohansemmanuel/f384456a7589816254c8b8297dad26be to your computer and use it in GitHub Desktop.
function TimerWithRefID() {
const setIntervalRef = useRef();
useEffect(() => {
const intervalID = setInterval(() => {
// something to be done every 100ms
}, 100);
// this is where the interval ID is saved in the ref object
setIntervalRef.current = intervalID;
return () => {
clearInterval(setIntervalRef.current);
};
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment