_.debounce(fn,waitTime,option) // 防抖 _.throttle(fn,waitTime,option) // 节流
2、自己封装
//防抖 function debounce(fn, delay) { let timer = null const _debounce = function () { if (timer) { clearTimeout(timer) } timer = setTimeout(() => { fn() }, delay) } return _debounce }
//节流 function throttle(fn, interval) { let lastTime = 0 const _throttle = function () { const nowTime = new Date().getTime() // console.log(new Date(nowTime)); const remainTime = interval - (nowTime - lastTime) if (remainTime <= 0) { fn() lastTime = nowTime } } return _throttle }