anitthro.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. let timer1 = null; //防抖,
  2. let timer2 = null; //节流
  3. function Debounce(fn, t) {//防抖
  4. let delay = t || 500;
  5. return function () {
  6. let args = arguments;
  7. // let timer1 = null
  8. // console.log(timer1);
  9. if(timer1){
  10. clearTimeout(timer1);
  11. }
  12. timer1 = setTimeout(() => {
  13. fn.apply(this, args);
  14. timer1 = null;
  15. }, delay);
  16. }
  17. }
  18. // 使用
  19. /*import {Debounce} from '@/common/debounceThrottle.js'
  20. Debounce(() => {
  21. //要执行的函数
  22. }, 200)() */
  23. function Throttle(fn, t) {//节流
  24. let last;
  25. let interval = t || 500;
  26. return function () {
  27. let args = arguments;
  28. let now = +new Date();
  29. if (last && now - last < interval) {
  30. clearTimeout(timer2);
  31. timer2 = setTimeout(() => {
  32. last = now;
  33. fn.apply(this, args);
  34. }, interval);
  35. } else {
  36. last = now;
  37. fn.apply(this, args);
  38. }
  39. }
  40. }
  41. module.exports = {
  42. Debounce: Debounce,
  43. Throttle: Throttle
  44. }