Files
time/src/utils/events.ts
T
2026-09-26 16:37:19 +03:00

27 lines
725 B
TypeScript

import { useEffect } from 'react'
export const useInterval = (fun: () => void, time: number) => {
useEffect(() => {
const _ = setInterval(fun, time)
return () => clearInterval(_)
}, [time, fun])
}
export const useTimeout = (fun: () => void, time: number) => {
useEffect(() => {
const _ = setTimeout(fun, time)
return () => clearTimeout(_)
}, [time, fun])
}
export const useWindowEventListener = <K extends keyof WindowEventMap>(
type: K,
call: (ev: WindowEventMap[K]) => void
) => {
useEffect(() => {
const listener = (ev: Event) => call(ev as WindowEventMap[K])
window.addEventListener(type, listener)
return () => window.removeEventListener(type, listener)
}, [type, call])
}