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
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
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#