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

19 lines
426 B
TypeScript
Raw Permalink 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,
}
}