element-plus/packages/hooks/use-timeout/index.ts

19 lines
426 B
TypeScript
Raw Normal View History

import { tryOnScopeDispose } from '@vueuse/core'
export function useTimeout() {
let timeoutHandle: number
const registerTimeout = (fn: (...args: any[]) => any, delay: number) => {
cancelTimeout()
timeoutHandle = window.setTimeout(fn, delay)
}
const cancelTimeout = () => window.clearTimeout(timeoutHandle)
tryOnScopeDispose(() => cancelTimeout())
return {
registerTimeout,
cancelTimeout,
}
}