debounce.js 605 B

1234567891011121314151617181920212223242526272829
  1. /**
  2. * 防止用户多次输入
  3. * @param cb 来源
  4. * @param delay 延迟时间
  5. * @returns {(function(...[*]): void)|*}
  6. */
  7. export function debounce(cb, delay) {
  8. let timer
  9. return function (...args) {
  10. if (timer) {
  11. clearTimeout(timer)
  12. }
  13. timer = setTimeout(() => {
  14. cb.call(this, ...args)
  15. }, delay)
  16. }
  17. }
  18. export function functionDebounce(cb, delay) {
  19. let timer
  20. return function () {
  21. if (timer) {
  22. clearTimeout(timer)
  23. }
  24. timer = setTimeout(() => {
  25. cb.call(this)
  26. }, delay)
  27. };
  28. }