JS 中防抖函数的实现

const debounce = (fun, time) => {
    let timer;
    return (...args) => {
        let context = this
        if(timer) {
           clearTimeout(timer)
        }
        timer = setTimeout(() => {
            fun.call(context, ...args)
        }, time)
    }
}

const event = debounce(function(e) {
    console.log(e.target.value)
})

const inpput = document.querySelector('input')
input.addEventListener('input', event )

你可能感兴趣的:(javascript,前端,开发语言)