Last Updated: February 25, 2016
·
12.37K
· 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 ·

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside