手写一个防抖函数

防抖,即短时间内大量触发同一事件,只会执行一次函数,实现原理为设置一个定时器,约定在xx毫秒后再触发事件处理,每次触发事件都会重新设置计时器,
直到xx毫秒内无第二次操作,防抖常用于搜索框/滚动条的监听事件处理,如果不做防抖,每输入一个字/滚动屏幕,都会触发事件处理,造成性能浪费。

function debounce(func, wait) {
    let timeout = null
    return function() {
        let context = this
        let args = arguments
        if (timeout) clearTimeout(timeout)
        timeout = setTimeout(() => {
            func.apply(context, args) //这步有点没有懂
        }, wait)
    }
}

本文看自:
https://juejin.im/post/5e8b261ae51d4546c0382ab4

猜你喜欢

转载自www.cnblogs.com/smart-girl/p/12656929.html