防抖
在肯定工夫内执行一次,如果在此工夫内再次触发,则从新计时
const debounce = (func, timeout, immediate = false) => {
let timer = null;
return function (...args) {if (!timer && immediate) {func.apply(this, args);
}
if (timer) {clearTimeout(timer);
}
timer = setTimeout(() => {func.apply(this, args);
}, timeout);
}
}
在肯定工夫内执行一次,如果在此工夫内再次触发,则会拦挡不执行
const throttle = (func, timeout) => {
let timer = null;
return function (...args) {if (!timer) {timer = setTimeout(() => {
timer = null;
func.apply(this, args);
}, timeout);
}
}
}