const exec = debounce(()=>{
console.log('time: ' + Date.now());
}, 3000);
function clickme(){
exec();
}
function debounce(fn,delay){
let timer = null
return function(...args) {
if(timer){
clearTimeout(timer);
}
timer = setTimeout(()=>{
fn.call(this, args);
},delay);
}
}
<p>
期望:在三秒内点击多次,只会执行一次
</p>
<button type="button" onclick="clickme()">点我</button>