mirror of
https://github.com/Stremio/stremio-web.git
synced 2026-03-11 21:27:05 +00:00
26 lines
555 B
TypeScript
26 lines
555 B
TypeScript
import { useEffect, useRef } from 'react';
|
|
|
|
const useInterval = (duration: number) => {
|
|
const interval = useRef<NodeJS.Timer | null>(null);
|
|
|
|
const start = (callback: () => void) => {
|
|
cancel();
|
|
interval.current = setInterval(callback, duration);
|
|
};
|
|
|
|
const cancel = () => {
|
|
interval.current && clearInterval(interval.current);
|
|
interval.current = null;
|
|
};
|
|
|
|
useEffect(() => {
|
|
return () => cancel();
|
|
}, []);
|
|
|
|
return {
|
|
start,
|
|
cancel,
|
|
};
|
|
};
|
|
|
|
export default useInterval;
|