element-plus/packages/hooks/use-timeout/index.ts
jeremywu 6b4c04fd0a
feat(hooks): add use-timeout hook (#2439)
* feat(hooks): add use-timeout hook

- Add use timeout hook for register timeout callbacks
- Add test cases against add-timeout

* resolve linter issue
2021-07-06 07:49:49 +08:00

21 lines
430 B
TypeScript

import { onBeforeUnmount } from 'vue'
export default function () {
let timeoutHandle: ReturnType<typeof setTimeout>
onBeforeUnmount(() => {
clearTimeout(timeoutHandle)
})
return {
registerTimeout: (fn: (...args: any[]) => unknown, delay: number) => {
clearTimeout(timeoutHandle)
timeoutHandle = setTimeout(fn, delay)
},
cancelTimeout: () => {
clearTimeout(timeoutHandle)
},
}
}