Take the 2-minute tour ×

Please tell me how to write javascript below in coffeescript.

}, 1000);
share|improve this question
I upvoted the question, not that it was exactly what I was looking for but the answers lead me in the right direction. –  MikeScott8 Feb 3 '12 at 22:44
This is a great question; it's one of a few that are helping me discover the limitations of Coffeescript (as it is now). –  thure Jul 10 '12 at 15:34

6 Answers 6

up vote 152 down vote accepted

I think it's a useful convention for callbacks to come as the last argument to a function. This is usually the case with the Node.js API, for instance. So with that in mind:

delay = (ms, func) -> setTimeout func, ms

delay 1000, -> something param

Granted, this adds the overhead of an extra function call to every setTimeout you make; but in today's JS interpreters, the performance drawback is insignificant unless you're doing it thousands of times per second. (And what are you doing setting thousands of timeouts per second, anyway?)

Of course, a more straightforward approach is to simply name your callback, which tends to produce more readable code anyway (jashkenas is a big fan of this idiom):

callback = -> something param
setTimeout callback, 1000
share|improve this answer
Great! I like the second approach. –  tomodian Jun 25 '11 at 6:03
Great unless you need nested callbacks –  Steven Pribilinskiy Dec 24 at 13:12
setTimeout ( ->
  something param
), 1000

The parentheses are optional, but starting the line with a comma seemed messy to me.

share|improve this answer
Thanks. Coffeescript sometimes screws me up because it's code is so clean. –  tomodian Jun 25 '11 at 6:05
Take out the parens, and have a cup of coffee ,~) –  Billy Moon Jun 27 '12 at 1:23
it's not clean if it screws you up, just sayin :) –  Kevin Sep 27 '12 at 5:16
This is the better answer here –  Zack Shapiro Jul 25 '13 at 15:14
Does not compile with the parentheses for me. I had to remove them, start the line with a comma, and it works like a charm. –  Elgringorrible Aug 7 at 13:29

This will result in a roughly equivalent translation (thanks @Joel Mueller):

setTimeout (-> something param), 1000

Note that this isn't an exact translation because the anonymous function returns the result of calling something(param) instead of undefined, as in your snippet.

share|improve this answer
Too many parens/semicolons! This is CoffeeScript, getting rid of excess parens is half the point. setTimeout (() -> something param), 1000 –  Joel Mueller Jun 23 '11 at 19:44
@Joel Mueller: thanks, I've updated my answer. –  maerics Jun 23 '11 at 19:46
Nicholas makes a good point that the empty parens are also optional. –  Joel Mueller Jun 23 '11 at 19:49
Thanks, I've used to messy code of Javascript, and Coffeescript sometimes confuses me. –  tomodian Jun 25 '11 at 6:10
setTimeout -> 
  something param
, 1000
share|improve this answer
This is how I write it. –  arbales Mar 5 '13 at 5:56
Only this method worked for me –  Dallas Clark Apr 22 '13 at 0:01
This is the best if you just want to use an anonymous function. –  Alex Kinnee Dec 12 '13 at 21:39
also for a named function, setTimeout namedFunc, 5000 –  Dirk Smaverson Dec 13 '13 at 1:26

I find this the best method to do the same,

setTimeout (-> alert "hi"), 1000
share|improve this answer

another option:

    -> something param
share|improve this answer

Your Answer


