SOURCE

console 命令行工具 X clear

                    
>
console
/**
 * 实现函数的防抖(目的是频繁触发中只执行一次)
 * @param {*} func 需要执行的函数
 * @param {*} wait 检测防抖的间隔频率
 * @param {*} immediate 是否是立即执行 True:第一次,默认False:最后一次
 * @return {可被调用执行的函数}
 */

function dbounce(func, wait = 500, immediate=false){
    let time= null;
    return function(...params){
        // 第一点击 没有设置过任何定时器 timer就要为 null
        let now = immediate && !time
        clearTimeout(time)
        time = setTimeout(_=>{
            time = null;
            !immediate ? func.call(this, ...params) : null
        },wait)
        now ? func.call(this, ...params) : null
    }
}


document.querySelector("#btn1").onclick=dbounce(function(){
    console.log('点你麻痹,停下来半秒我不就显示了嘛')
})


document.querySelector("#btn2").onclick=dbounce(function(){
    console.log('点你麻痹,等会再点')
},500, true)
<button id="btn1">连续点击看看1</button>

<button id="btn2">连续点击看看2</button>