1. // 防抖
    2. const debounce = (fn, delay) => {
    3. let timer = null
    4. return function () {
    5. const args = arguments
    6. clearTimeout(timer)
    7. timer = setTimeout(() => {
    8. fn.apply(this, args)
    9. }, delay)
    10. }
    11. }
    12. // 节流
    13. const throttle = (fn, delay) => {
    14. let timer = null
    15. return function () {
    16. const args = arguments
    17. if (timer) return
    18. timer || fn.apply(this, args)
    19. timer = setTimeout(() => {
    20. timer = null
    21. }, wait)
    22. }
    23. }