mirror of
https://github.com/cheeaun/phanpy.git
synced 2024-12-24 03:08:13 +03:00
22 lines
585 B
JavaScript
22 lines
585 B
JavaScript
import { useEffect, useRef } from 'preact/hooks';
|
|
|
|
function useInterval(fn, delay, deps, immediate) {
|
|
const savedCallback = useRef(fn);
|
|
useEffect(() => {
|
|
savedCallback.current = fn;
|
|
}, [fn, deps]);
|
|
|
|
useEffect(() => {
|
|
if (!immediate || delay === null || delay === false) return;
|
|
savedCallback.current();
|
|
}, [immediate]);
|
|
|
|
useEffect(() => {
|
|
if (delay === null || delay === false) return;
|
|
const tick = () => savedCallback.current();
|
|
const id = setInterval(tick, delay);
|
|
return () => clearInterval(id);
|
|
}, [delay]);
|
|
}
|
|
|
|
export default useInterval;
|