63.throttle
webVueBlog opened this issue · comments
I am ne zha / Jeskson commented
// 节流
// 原理:事件被频繁触发时,事件回调函数会按照固定频率执行,比如1s 执行一次,只有上个事件回调被执行之后下一个事件回调才会执行
// 事件回调函数
// wait 事件回调的执行频率,每wait毫秒执行一次
function throttle(fn, await = 500) {
let timer = null
return function(...args) {
if (timer) return
timer = setTimeout(() => {
fn.apply(this, args)
timer = null
}, await)
}
}