Ad
  • Default User Avatar

    it might be, that the passed function invokes the function again.

    e.g.

    myDiv.onclick = once ->
      console.log "hello world"
      myDiv.onclick() # recursive loops ftw, all your CPU are belong to us
    

    would run the myDiv.onclick more than once if you place shouldRun = false AFTER executing the function. This is why you should place it first.

  • Custom User Avatar

    if place fn.apply(this, arguments) before shouldRun = false the test will fail.
    thus~

    once = (fn) ->
      shouldRun = true
      () ->
        if shouldRun
          fn.apply(this, arguments)
          shouldRun = false
    

    any idea?

  • Custom User Avatar

    oops~ it is so cool and tricky