当达到了一定的时间间隔就会执行一次;可以理解为是缩减执行频率
节流函数:
export const throttle = (fn, delay, mustRunDelay) => {
let timer = null
let t_start
return function() {
const context = this
const args = arguments
let t_curr = +new Date()
clearTimeout(timer)
if (!t_start) {
t_start = t_curr
}
if (t_curr - t_start >= mustRunDelay) {
fn.apply(context, args)
t_start = t_curr
} else {
timer = setTimeout(function() {
fn.apply(context, args)
}, delay)
}
}
};
使用:页面中需要触发的函数,用节流函数来控制
inputChange:throttle(function(e) {
...... 此处省略逻辑
},300)
博客探讨了JavaScript中的节流函数,用于控制函数执行频率,确保在输入变化时不会过于频繁地触发处理逻辑,例如在inputChange事件中,通过设置300ms的延迟,实现了对函数调用的优化,提高了页面性能。

4816

被折叠的 条评论
为什么被折叠?



