0

This is my CoffeeScript code.

setTimeout (-> @checkProgress()), 5000   

When I run this in the browser I get the following error:

TypeError: this.checkProgress is not a function

The method looks like:

checkProgress: ->
    ~ code
    ~ code
    ~ code
    setTimeout (-> @checkProgress()), 5000   

So at some point I want to call the method again. How can I do this? Thanks.

3
  • @checkProgress is already a function. You do not need to wrap it in another function. Just use setTimeout @checkProgress, 5000 Commented Aug 11, 2016 at 4:10
  • In other cases you can use helper: delay = (t, fn) -> setTimeout(fn, t). Commented Aug 11, 2016 at 4:12
  • That's what I was doing before but then I read that when doing that the actual function doesn't get called but rather the result of the function ends up in that param position which isn't what I wanted. Commented Aug 11, 2016 at 22:04

2 Answers 2

2

setTimeout run @checkProgress in window context. Use fat arrow:

setTimeout (() => @checkProgress), 5000
Sign up to request clarification or add additional context in comments.

1 Comment

This worked, sort of. I ended up getting a different error. Undefined for the variables i was referencing in the method code.
0

This personally worked perfectly for me as well.

recall = =>
          @checkProgress()
        setTimeout recall, 5000

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.