js debounce防抖技术

今天写搜索组件的时候,使用到了防抖技术,之前一直没有做笔记,今天有空就记录一下

export default function debounce(func, delay) {
    let timer;
    return function (...args) {
        if (timer) {
            clearTimeout(timer)
        }
        timer = setTimeout(() => {
            func.apply(this, args)
        }, delay)
    }
}

 

猜你喜欢

转载自www.cnblogs.com/maomao93/p/12552247.html
今日推荐