1

I'm trying to implement the code from this SO question in coffeescript.

This is what I've got but it doesn't work.

class SomeClass

    initialize: ->

      target = view.$('#finder')

      typewatch = do ->
        timer = 0;
        (callback, ms) ->
          clearTimeout timer
          timer = setTimeout callback, ms

      target.keyup =>
        typewatch @someMethod(target.val()), 1000


    someMethod: (arg) ->
      // do something

someMethod gets called without any delay. It seems like setTimeout isn't being called at all.

Community
  • 1
  • 1
niftygrifty
  • 3,452
  • 2
  • 28
  • 49

1 Answers1

1

@someMethod() grammar does invoke the method immediately.

If you want to "store" a method invocation (roughly, method+this+arguments) in a variable and call it later, use Function.prototype.bind. e.g. typewatch @someMethod.bind(@, target.val()), 1000

BTW as elclanrs mentioned, the setTimeout (->callback) part won't really execute callback.

Jokester
  • 5,501
  • 3
  • 31
  • 39