ant-design-vue/components/form/utils/useDebounce.ts
2022-03-17 21:53:14 +08:00

19 lines
439 B
TypeScript

import type { Ref } from 'vue';
import { shallowRef, watchEffect } from 'vue';
export default function useDebounce<T>(value: Ref<T[]>): Ref<T[]> {
const cacheValue = shallowRef(value.value.slice());
let timeout: any = null;
watchEffect(() => {
clearTimeout(timeout);
timeout = setTimeout(
() => {
cacheValue.value = value.value;
},
value.value.length ? 0 : 10,
);
});
return cacheValue;
}