common.js 745 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. // 防抖
  2. export const Debounce = (fn, t) => {
  3. let delay = t || 500;
  4. let timer;
  5. console.log(fn)
  6. console.log(typeof fn)
  7. return function () {
  8. let args = arguments;
  9. if(timer){
  10. clearTimeout(timer);
  11. }
  12. timer = setTimeout(() => {
  13. timer = null;
  14. fn.apply(this, args);
  15. }, delay);
  16. }
  17. };
  18. // 节流
  19. export const Throttle = (fn, t) => {
  20. let last;
  21. let timer;
  22. let interval = t || 500;
  23. return function () {
  24. let args = arguments;
  25. let now = +new Date();
  26. if (last && now - last < interval) {
  27. clearTimeout(timer);
  28. timer = setTimeout(() => {
  29. last = now;
  30. fn.apply(this, args);
  31. }, interval);
  32. } else {
  33. last = now;
  34. fn.apply(this, args);
  35. }
  36. }
  37. };