Skip to content

13 - Slide in on Scroll #19

Description

缉熙你好,该程序中的 debounce 防抖 我觉得应该是 throttle 节流

debounce 防抖

debounce 防抖 应该是在用户停止触发某个事件一段时间后,该事件的处理程序被触发。

比如输入用户名时的验证,应该等到用户输入完成,也就是停止输入后才触发,而不是在用户每次输入一段时间后就触发验证程序。用户名有长有短,不能在用户还未输入完成时就提示错误。

throttle 节流

throttle 节流 则是限制事件处理程序被调用的频率,在固定的时间内只会调用一次。

该程序只是为了限制 scroll 事件处理程序被调用的次数,这里应该是 throttle 节流

但我注意看了该程序的作用,确实是限制了 scroll 事件处理程序被调用的次数,那么就是名字起错了。

这里贴一段稍微容易理解的 throttle 节流 函数,该程序中的写法感觉好复杂:

function throttle (func, interval) {
  let startTime = Date.now()

  return function (...args) {
    const currentTime = Date.now()
    if (currentTime - startTime > interval) {
      func(...args)
      startTime = currentTime
    }
  }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions