【JavaScript】手写节流

【JavaScript】手写节流

// 概念:触发CD

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

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