debounce.js 859 B

123456789101112131415161718192021222324252627282930313233
  1. /**
  2. * 函数防抖: 当事件被触发 n 秒后再执行回调,如果在 n 秒内又被触发,则重新计时。
  3. * @returns {(function(): void)|*}
  4. * @param func
  5. * @param wait
  6. */
  7. // export const debounce = (func, wait) => {
  8. // let delay = wait || 500;
  9. // let timer = null;
  10. // return () => {
  11. // const _this = this;
  12. // let args = arguments;
  13. // if(timer) clearTimeout(timer);
  14. // timer = setTimeout(() => {
  15. // timer = null;
  16. // func.apply(_this, args);
  17. // }, delay);
  18. // };
  19. // };
  20. export function debounce(fn, timer){
  21. // console.log("fn", fn);
  22. let timer_out = null;
  23. return function(...args){
  24. // 清除计时器
  25. if(timer_out) clearTimeout(timer_out);
  26. timer_out = setTimeout(()=>{
  27. fn.apply(this,args);
  28. },timer);
  29. };
  30. }