防抖函数实现

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), [])
Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐