ChickenDreamFactory / fe-chicken

✨✨✨ 集锦 前端JavaScript 手写题,编程题,Not just for interviews

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

63.throttle

webVueBlog opened this issue · comments

// 节流
// 原理:事件被频繁触发时,事件回调函数会按照固定频率执行,比如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)
 }
}