防抖
触发高频时间后n内后函数只会执行一次,如果n秒内高频时间再触发,则重新计算是时间
const debounce = (fn, time) => {
let timeout = null;
return function () {
clearTimeout(timeout);
timeout = setTimeout(() => {
fn.apply(this, arguments);
}, time)
}
}
节流
高频时间触发,但n秒内只会执行一次,所以节流会稀释函数的执行频率
注:节流常用于鼠标不断点击触发、监听滚动事件
const throttle = (fn, time) => {
let flag = true;
return function () {
if (!flag) return;
flag = false;
setTimeout(() => {
fn.apply(this, arguments);
flag = true;
}, time);
}
}