refactor: ♻️ 删除重复的防抖函数定义,统一使用 vueuse 防抖函数

This commit is contained in:
ray
2024-11-23 17:17:37 +08:00
parent 4528d83a81
commit 687eca10a2
3 changed files with 6 additions and 36 deletions

View File

@@ -39,32 +39,3 @@ export function isExternal(path: string) {
const isExternal = /^(https?:|http?:|mailto:|tel:)/.test(path);
return isExternal;
}
/**
* 防抖函数
*
* @param {Function} fn 需要防抖的函数
* @param {number} delay 防抖时间
* @returns {Function}
*/
export const debounce = <T extends (...args: any[]) => any>(
fn: T,
delay: number,
immediate = false
) => {
let timer: NodeJS.Timeout | null = null;
return function (this: any, ...args: Parameters<T>) {
if (timer) clearTimeout(timer);
if (immediate && !timer) {
fn.apply(this, args);
}
timer = setTimeout(() => {
if (!immediate) {
fn.apply(this, args);
}
timer = null;
}, delay);
};
};

View File

@@ -136,7 +136,6 @@ defineOptions({
});
import ConfigAPI, { ConfigPageVO, ConfigForm, ConfigPageQuery } from "@/api/system/config";
import { debounce } from "@/utils";
const queryFormRef = ref(ElForm);
const dataFormRef = ref(ElForm);
@@ -212,8 +211,8 @@ function handleOpenDialog(id?: number) {
}
}
// 防抖刷新缓存
const debouncedRefresh = debounce(() => {
// 刷新缓存(防抖)
const debouncedRefresh = useDebounceFn(() => {
ConfigAPI.refreshCache().then(() => {
ElMessage.success("刷新成功");
});

View File

@@ -380,9 +380,9 @@ function handleCloseDialog() {
formData.status = 1;
}
// 提交用户表单(防抖处理
const handleSubmit = useThrottleFn(() => {
userFormRef.value.validate((valid: any) => {
// 提交用户表单(防抖)
const handleSubmit = useDebounceFn(() => {
userFormRef.value.validate((valid: boolean) => {
if (valid) {
const userId = formData.id;
loading.value = true;
@@ -405,7 +405,7 @@ const handleSubmit = useThrottleFn(() => {
}
}
});
}, 3000);
}, 1000);
/**
* 删除用户