延迟执行
function debounce(wait) {
var timer = null;
return function (fn) {
if (timer !== null) {
clearTimeout(timer);
}
timer = setTimeout(fn, wait);
};
}
let debounced = debounce(3000);
debounced(()=>{
console.log("1");
})
setTimeout(()=>{
debounced(()=>{
console.log("2");
})
},2000)
标签:function,setTimeout,函数,debounced,js,timer,null,log
From: https://www.cnblogs.com/yuyw/p/16753628.html