/
githubmirror
/
material-ui
Обзор
Документация
Войти
/
githubmirror
/
material-ui
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
packages/mui-utils/src/useTimeout/useTimeout.ts
41 строка
833 B
Olivier Tassinari
[core] Use runtime agnostic setTimeout type
25 фев 2024, 04:52
Не верифицирован
25 фев 2024, 04:52
046aac6
Код
Авторство
О чём код?
'use client'; import useLazyRef from '../useLazyRef/useLazyRef'; import useOnMount from '../useOnMount/useOnMount'; export class Timeout { static create() { return new Timeout(); } currentId: ReturnType<typeof setTimeout> | null = null; /** * Executes `fn` after `delay`, clearing any previously scheduled call. */ start(delay: number, fn: Function) { this.clear(); this.currentId = setTimeout(() => { this.currentId = null; fn(); }, delay); } clear = () => { if (this.currentId !== null) { clearTimeout(this.currentId); this.currentId = null; } }; disposeEffect = () => { return this.clear; }; } export default function useTimeout() { const timeout = useLazyRef(Timeout.create).current; useOnMount(timeout.disposeEffect); return timeout; }