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!"
Written by Thinh Tran Duy
Related protips
2 Responses
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
Best
#Coffeescript
Authors
iam4x
94.17K
ericdfields
63.02K
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#