函数节流是指规定一个单位时间,在这个单位时间内,只能有一次触发事件的回调函数执行,如果在同一个单位时间内某事件被触发多次,只有一次能生效。节流可以使用在 scroll 函数的事件监听上,通过事件节流来降低事件调用的频率。
实现方式1(时间戳实现)
function throttle(fn, delay) {
let curTime = Date.now();
return function() {
let context = this,
args = arguments,
nowTime = Date.now();
// 如果两次时间间隔超过了指定时间,则执行函数。
if (nowTime - curTime >= delay) {
curTime = Date.now();
return fn.apply(context, args);
}
};
}
实现方式2(setTimeout实现)
function throttled2(fn, delay = 500) {
let timer = null;
return function (...args) {
const context = this
if (!timer) {
timer = setTimeout(() => {
fn.apply(context, args)
timer = null;
}, delay)
}
}
}