2021-11-29 15:58:44 +08:00
|
|
|
import { tryOnScopeDispose } from '@vueuse/core'
|
2021-07-06 07:49:49 +08:00
|
|
|
|
2021-11-29 15:58:44 +08:00
|
|
|
export function useTimeout() {
|
|
|
|
let timeoutHandle: number
|
2021-08-24 13:36:48 +08:00
|
|
|
|
2021-11-29 15:58:44 +08:00
|
|
|
const registerTimeout = (fn: (...args: any[]) => any, delay: number) => {
|
|
|
|
cancelTimeout()
|
|
|
|
timeoutHandle = window.setTimeout(fn, delay)
|
|
|
|
}
|
|
|
|
const cancelTimeout = () => window.clearTimeout(timeoutHandle)
|
2021-07-06 07:49:49 +08:00
|
|
|
|
2021-11-29 15:58:44 +08:00
|
|
|
tryOnScopeDispose(() => cancelTimeout())
|
2021-07-06 07:49:49 +08:00
|
|
|
|
|
|
|
return {
|
2021-11-29 15:58:44 +08:00
|
|
|
registerTimeout,
|
|
|
|
cancelTimeout,
|
2021-07-06 07:49:49 +08:00
|
|
|
}
|
|
|
|
}
|