当达到了一定的时间间隔就会执行一次;可以理解为是缩减执行频率
节流函数:
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)