plm前端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

31 lines
619 B

2 years ago
  1. // 防抖
  2. export function debounce(fn, wait = 3000) {
  3. let timer = null;
  4. return function () {
  5. console.log(wait)
  6. let args = arguments;
  7. let that = this;
  8. if (timer) {
  9. clearTimeout(timer)
  10. }
  11. timer = setTimeout(function () {
  12. timer = null;
  13. fn.apply(that, args)
  14. }, wait)
  15. }
  16. }
  17. // 节流
  18. export function throttle(fn, wait = 3000) {
  19. let timer = null;
  20. return function () {
  21. if (timer != null) return;
  22. let args = arguments;
  23. let that = this;
  24. clearTimeout(timer)
  25. fn.apply(that, args)
  26. timer = setTimeout(function () {
  27. timer = null;
  28. }, wait)
  29. }
  30. }