// 节流是指:让其在第一次触发事件 // 防抖是指:让其在最后一次触发事件 // fn对应的时间处理函数,delay间隔时间,immediate第一次是否立即执行 // 防抖 const debounce = function(fn, delay, immediate) { let timer = null return function() { const that = this const arg = arguments if (timer) { clearTimeout(timer) } if (immediate) { const bool = !timer timer = setTimeout(() => { timer = null }, delay) if (bool) { fn.apply(that, arg) } } else { timer = setTimeout(() => { timer = null fn.apply(that, arg) }, delay) } } } // 节流 const throttle = function(fn, delay, immediate) { let timer = null return function() { const that = this const arg = arguments if (immediate) { if (!timer) { timer = setTimeout(() => { timer = null }, delay) fn.apply(that, arg) } } else { if (!timer) { timer = setTimeout(() => { timer = null fn.apply(that, arg) }, delay) } } } } export { debounce, throttle }