SOURCE

/**
 * @desc 防抖函数
 * @params 接受两个参数:1. 需要防抖的函数fn 2. 防抖的时间
 * @return 返回一个函数
*/
function debounce(fn, delay) {
    let timer;
    return function(...args) {
        // 如果有进行中的定时器,需要先清除一下
        if (timer) {
            clearTimeout(timer);
        }
        timer = setTimeout(() => {
           fn.apply(this, args)
        }, delay)
    }
}

/**
 * @desc 节流函数
 * @params 接受两个参数 1. 需要节流的函数 2. 节流的时间
 * @return 返回一个函数
*/
function throttle(fn, wait) {
    let canRun = true;
    return function (...args) {
        // 相当于上了一个锁
        if (!canRun) {
            return
        }
        canRun = false;
        setTimeout(() => {
            fn.apply(this, args);
            // 解锁
            canRun = true;
        }, wait)
    }
}
console 命令行工具 X clear

                    
>
console