useTimeout.ts 554 字节
Newer Older
陈文彬 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
import type { TimeoutFnResult, Fn } from './types';

import { isFunction } from '/@/utils/is';
import { watch } from 'vue';

import { useTimeoutRef } from '/@/hooks/core/useTimeoutRef';

export function useTimeout(handle: Fn<any>, wait: number): TimeoutFnResult {
  if (!isFunction(handle)) {
    throw new Error('handle is not Function!');
  }

  const [readyRef, clear, runAgain] = useTimeoutRef(wait);

  watch(
    readyRef,
    (maturity) => {
      maturity && handle();
    },
    { immediate: false }
  );
  return [clear, runAgain, readyRef];
}