Skip to content

Commit

Permalink
update hook useInterval
Browse files Browse the repository at this point in the history
  • Loading branch information
xizon committed Aug 29, 2024
1 parent ff4682f commit d35060a
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions src/utils/hooks/useInterval.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,48 @@
const App = () => {
const [count, setCount] = useState(0);
useInterval(() => {
const { startTimer, stopTimer } = useInterval(() => {
setCount(count + 1);
}, 1000);
return (
<div className="app"></div>
<div className="app">{count}</div>
);
};
*/
import { useEffect, useRef } from "react";
import { useEffect, useRef, useCallback } from "react";

const useInterval = (fn, delay) => {
const ref = useRef(null);

const intervalIdRef = useRef(null);
const startTimer = useCallback(() => {
intervalIdRef.current = setInterval(() => {
ref.current && ref.current();
}, delay);
}, [ref]);

const stopTimer = useCallback(() => {
clearInterval(intervalIdRef.current);
intervalIdRef.current = null;
}, []);

useEffect(() => {
ref.current = fn;
}, [fn]);

useEffect(() => {
function tick() {
ref.current && ref.current();
}

if (delay !== null && delay > 0) {
let id = setInterval(tick, delay);
return () => clearInterval(id);
} else {
tick();
}
}, [delay]);
startTimer();
return () => stopTimer();
}, []);


return {
startTimer,
stopTimer
};

};

export default useInterval;
Expand Down

0 comments on commit d35060a

Please sign in to comment.