防抖、节流 函数

节流

function throttle(fn, interval) {
  let enterTime = 0 // 触发的时间
  const gapTime = interval || 1000 // 间隔时间,如果interval不传,则默认1000ms
  return function() {
    const context = this
    const backTime = new Date() // 第一次函数return即触发的时间
    if (backTime - enterTime > gapTime) {
      fn.call(context, arguments)
      enterTime = backTime // 赋值给第一次触发的时间,这样就保存了第二次触发的时间
    }
    console.log('节流成功!')
  }
}

 

防抖 

function debounce(fn, interval) {
  let timer
  const gapTime = interval || 1000 // 间隔时间,如果interval不传,则默认1000ms
  return function() {
    clearTimeout(timer)
    const context = this
    const args = arguments // 保存此处的arguments,因为setTimeout是全局的,arguments不是防抖函数需要的。
    timer = setTimeout(function() {
      fn.call(context, args)
    }, gapTime)
    console.log('防抖成功!')
  }
}

 

posted @ 2022-09-19 20:06  收破烂的小伙子  阅读(22)  评论(0编辑  收藏  举报