SOURCE

/**
 * @Desc debounce 防抖, 每次执行都重新算时间
 * @params 接收两个参数,需要防抖的函数,时间
 * @returns 返回一个新的函数
*/
function debounce(fn, delay) {
    let timer;
    return function(...args) {
        // 如果在本个周期还没开始执行之前又调用了该函数,则重新计时
        if (timer) {
            clearTimeout(timer);
        }
        setTimeout(() => {
            fn.apply(this, args)
        }, delay)
    }
}

/**
 * @desc throttle 节流
*/
function throttle(fn, wait) {
    let canRun = true;
    // 内部有一个 canRun 的判断条件,只有等上次执行完了,下一次才会允许执行
    return function(...args) {
        if (!canRun) {
            return;
        }
        setTimeout(() => {
            fn.apply(this, args)
        }, wait)
    }
}
console 命令行工具 X clear

                    
>
console