react 函数组件 防抖操作

 1   function deBounce(fn: { apply: (value: any, arg1: any) => void }, t?: number) {
 2     let timeId: any = null;
 3     const delay = t || 500;
 4     return function (this: any, ...args: any) {
 5       if (timeId) {
 6         clearTimeout(timeId);
 7       }
 8       timeId = setTimeout(() => {
 9         timeId = null;
10         fn.apply(this, args);
11       }, delay);
12     };
13   }
14 
15   const urlValidate = useCallback(
16     deBounce(async (v, i) => {
17       console.log(v, i);
18       // axios({
19       //   url: `/api/xxxxx`,
20       //   method: 'POST',
21       //   data: v,
22       // }).then(res => {
23       //     // 接口数据处理
24       // );
25     }, 350),
26     [],
27   );

 

posted @ 2023-02-14 14:50  lrt-web  阅读(39)  评论(0)    收藏  举报