| 123456789101112131415161718192021222324252627282930313233 |
- /**
- * 函数防抖: 当事件被触发 n 秒后再执行回调,如果在 n 秒内又被触发,则重新计时。
- * @returns {(function(): void)|*}
- * @param func
- * @param wait
- */
- // export const debounce = (func, wait) => {
- // let delay = wait || 500;
- // let timer = null;
- // return () => {
- // const _this = this;
- // let args = arguments;
- // if(timer) clearTimeout(timer);
- // timer = setTimeout(() => {
- // timer = null;
- // func.apply(_this, args);
- // }, delay);
- // };
- // };
- export function debounce(fn, timer){
- // console.log("fn", fn);
- let timer_out = null;
- return function(...args){
- // 清除计时器
- if(timer_out) clearTimeout(timer_out);
- timer_out = setTimeout(()=>{
- fn.apply(this,args);
- },timer);
- };
- }
|