-
Notifications
You must be signed in to change notification settings - Fork 0
函数节流和防抖
jerryzhang edited this page Jan 31, 2023
·
1 revision
title: 函数节流和防抖 url: https://www.yuque.com/endday/blog/wb53ai
触发高频事件后n秒内函数只会执行一次,如果n秒内高频事件再次被触发,则重新计算时间
每次触发事件时都取消之前的延时调用方法
function debounce(fn, delay = 500) {
let timeout = null; // 创建一个标记用来存放定时器的返回值
return function () {
clearTimeout(timeout); // 每当用户输入的时候把前一个 setTimeout clear 掉
timeout = setTimeout(() => { // 然后又创建一个新的 setTimeout, 这样就能保证输入字符后的 interval 间隔内如果还有字符输入的话,就不会执行 fn 函数
fn.apply(this, arguments);
}, delay);
};
}
function deBounce<T>(fn: T, delay: number): () => void {
let timer: NodeJS.Timeout
return function(): void {
const args: any[] = Array.prototype.map.call(arguments, val => val);
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(() => {
typeof fn === 'function' && fn.apply(null, args);
clearTimeout(timer);
}, delay > 0 ? delay : 100);
}
}
高频事件触发,但在n秒内只会执行一次,所以节流会稀释函数的执行频率
每次触发事件时都判断当前是否有等待执行的延时函数
function throttle(fn, delay = 500) {
let canRun = true; // 通过闭包保存一个标记
return function () {
if (!canRun) return; // 在函数开头判断标记是否为true,不为true则return
canRun = false; // 立即设置为false
setTimeout(() => { // 将外部传入的函数的执行放在setTimeout中
fn.apply(this, arguments);
// 最后在setTimeout执行完毕后再把标记设置为true(关键)表示可以执行下一次循环了。当定时器没有执行的时候标记永远是false,在开头被return掉
canRun = true;
}, delay);
};
}