Last Updated: February 25, 2016
·
12.06K
· duythinht

Sleep in CoffeeScript

Most of programming language have sleep() function, but Javascript doesn't. Well, if sometime we need use sleep for simulating heavy processing and for misc performance measurements, it could be useful. So here's how you can go about creating a sleep() in CoffeeScript.

sleep = (ms) ->
  start = new Date().getTime()
  continue while new Date().getTime() - start < ms

After compiled to JavaScript:

var sleep;
sleep = function(ms) {
  var start, _results;
  start = new Date().getTime();
  while (new Date().getTime() - start < ms) {
    continue;
  }
};

Test in action:

console.log "Test sleep begin, the program will sleep in 4 seconds"
sleep 4000
console.log "Done!"

2 Responses
Add your response

very handy, thanks for posting this!

over 1 year ago ·

Thank you, very useful!

over 1 year ago ·