mirror of
https://github.com/cheeaun/phanpy.git
synced 2024-12-23 00:30:14 +03:00
25 lines
610 B
JavaScript
25 lines
610 B
JavaScript
import { useEffect, useRef } from 'preact/hooks';
|
|
|
|
const noop = () => {};
|
|
|
|
function useInterval(callback, delay, immediate) {
|
|
const savedCallback = useRef(noop);
|
|
|
|
useEffect(() => {
|
|
savedCallback.current = callback;
|
|
}, []);
|
|
|
|
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;
|