sepfia
Last Updated: February 25, 2016
·
7.504K
· duythinht
C40ba82a0972879815ab7a1834168adf

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!"
Say Thanks
Respond

2 Responses
Add your response

15709
75fb60cc633a994debfe8b3a19eb91cb

very handy, thanks for posting this!

over 1 year ago ·
22668
None

Thank you, very useful!

over 1 year ago ·