console
function dbounce(func, wait = 500, immediate=false){
let time= null;
return function(...params){
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>