// 节流 - 是在高频出发函数的时候,如果当前函数正在执行,则再触发的函数就不执行了
let canRun = true;
function onScroll() {
if (!canRun) {
return;
}
canRun = false;
setTimeout(() => {
canRun = true;
}, 300);
}
// 防抖 - 是高频发生时,只执行最后一次,前面的都被取消执行了
let timer;
function onScroll() {
clearTimeout(timer);
timer = setTimeout(() => {
console.log('防抖');
}, 300);
}
防抖和节流
最新推荐文章于 2022-08-27 22:08:55 发布