Skip to content

Commit

Permalink
*
Browse files Browse the repository at this point in the history
  • Loading branch information
imteekay committed Apr 21, 2024
1 parent 69d5c36 commit a46ec33
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions coding_interviews/frontend/react/useInterval.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { useEffect, useRef } from 'react';

function useInterval(callback, delay) {
const callbackRef = useRef();

useEffect(() => {
callbackRef.current = callback;
}, [callback]);

useEffect(() => {
const interval = setInterval(() => {
callbackRef.current();
}, delay);

if ([null, undefined].includes(delay)) {
clearInterval(interval);
}

return () => {
clearInterval(interval);
};
}, [delay]);
}

0 comments on commit a46ec33

Please sign in to comment.