防抖函数实现
export function deBounce (fn: { apply: (arg0: any, arg1: any) => void }, t?: number) {
let timeId: any = null
const delay = t || 500
return function (this: any, ...args: any) {
if (timeId) {
clearTimeout(timeId)
}
timeId = setTimeout(() => {
timeId = null
fn.apply(this, args)
}, delay)
}
}
模块内调用
const urlValidate = useCallback(deBounce(async (v, i) => {
axios({
url: `/api/xxxxx`,
method: 'POST',
data: v,
}).then(res => {
// 接口数据处理
);
}, 200), [])