发布于 2016-05-29 08:20:14 | 188 次阅读 | 评论: 0 | 来源: 网络整理
你想只执行某个函数一次,在开始或结束时把多个连续的调用合并成一个简单的操作。
使用一个命名函数:
debounce: (func, threshold, execAsap) ->
  timeout = null
  (args...) ->
    obj = this
    delayed = ->
      func.apply(obj, args) unless execAsap
      timeout = null
    if timeout
      clearTimeout(timeout)
    else if (execAsap)
      func.apply(obj, args)
    timeout = setTimeout delayed, threshold || 100
mouseMoveHandler: (e) ->
  @debounce((e) ->
    # 只能在鼠标光标停止 300 毫秒后操作一次。
  300)
someOtherHandler: (e) ->
  @debounce((e) ->
    # 只能在初次执行 250 毫秒后操作一次。
  250, true)
可参阅 John Hann 的博客文章,了解 JavaScript 去抖动方法。