一 防抖
防抖函数 debounce 指的是某个函数在某段时间内,无论触发了多少次回调,都只执行最后一次。假如我们设置了一个等待时间 3 秒的函数,在这 3 秒内如果遇到函数调用请求就重新计时 3 秒,直至新的 3 秒内没有函数调用请求,此时执行函数,不然就以此类推重新计时。
原理及实现
实现原理就是利用定时器,函数第一次执行时设定一个定时器,之后调用时发现已经设定过定时器就清空之前的定时器,并重新设定一个新的定时器,如果存在没有被清空的定时器,当定时器计时结束后触发函数执行。
实现 1
// 实现 1// fn 是需要防抖处理的函数// wait 是时间间隔function debounce(fn, wait = 50) {// 通过闭包缓存一个定时器 idlet timer = null// 将 debounce 处理结果当作函数返回// 触发事件回调时执行这个返回函数return function() {let self = thislet args = arguments;// 如果已经设定过定时器就清空上一次的定时器if (timer) clearTimeout(timer)// 开始设定一个新的定时器,定时器结束后执行传入的函数 fntimer = setTimeout(function() {fn.apply(self, args)}, wait)}}// DEMO// 执行 debounce 函数返回新函数const betterFn = debounce(() => console.log('fn 防抖执行了'), 1000)// 停止滑动 1 秒后执行函数 () => console.log('fn 防抖执行了')document.addEventListener('scroll', betterFn)
实现 2
实现原理比较简单,判断传入的 immediate 是否为 true,另外需要额外判断是否是第一次执行防抖函数,判断依旧就是 timer 是否为空,所以只要 immediate && !timer 返回 true 就执行 fn 函数,即 fn.apply(this, args)。
// 实现 2// immediate 表示第一次是否立即执行function debounce(func, wait = 50, immediate) {var timer, result;var debounced = function () {var context = this;var args = arguments;if (timer) clearTimeout(timer);if (immediate) {// 如果已经执行过,不再执行var callNow = !timer;// 最后停止后 过了 wait 时间在执行 并清除timertimer = setTimeout(function(){timer = null;result = func.apply(context, args)}, wait)if (callNow) result = func.apply(context, args)}else {timer = setTimeout(function(){timer = null;func.apply(context, args)}, wait);}return result;};// 取消防抖debounced.cancel = function() {clearTimeout(timer);timer = null;};// 返回值return debounced;}// DEMO// 执行 debounce 函数返回新函数const betterFn = debounce(() => console.log('fn 防抖执行了'), 1000, true)// 第一次触发 scroll 执行一次 fn,后续只有在停止滑动 1 秒后才执行函数 fndocument.addEventListener('scroll', betterFn)
二 节流
使用时间戳实现
当触发事件的时候,我们取出当前的时间戳,然后减去之前的时间戳(最一开始值设为 0 ),如果大于设置的时间周期,就执行函数,然后更新时间戳为当前的时间戳,如果小于,就不执行。
function throttle(func, wait) {var context, args;var previous = 0;return function() {var now = +new Date();context = this;args = arguments;if (now - previous > wait) {func.apply(context, args);previous = now;}}}
使用定时器
function throttle(func, wait) {var timeout;var previous = 0;return function() {context = this;args = arguments;if (!timeout) {timeout = setTimeout(function(){timeout = null;func.apply(context, args)}, wait)}}}
