【JavaScript】手写防抖

【JavaScript】手写防抖

// 概念:回城被打断

let debounce = (fn, time, asThis) => {
    let timer = null;
    return (...args) => {
        if (timer) {
            clearTimeout(timer)
        }
        timer = setTimeout(() => {
            fn.call(asThis, ...args)
            timer = null
        }, time)
    }
}

const cp = debounce(() => {
    console.log('调用成功');
}, 4000);
LICENSED UNDER CC BY-NC-SA 4.0