JavaScript sleep Function
The infamous sleep
, or delay
, function within any language is much debated. Some will say that there should always be a signal or callback to fire a given functionality, others will argue that sometimes an arbitrary moment of delay is useful. I say that to each their own and one rule can never dictate anything in this industry.
Writing a sleep
function is simple and made even more usable with JavaScript Promises:
// https://zeit.co/blog/async-and-await
function sleep (time) {
return new Promise((resolve) => setTimeout(resolve, time));
}
// Usage!
sleep(500).then(() => {
// Do something after the sleep!
})
Without promises you'd need to pass a callback; with our beautiful promises we simply resolve after the setTimeout
and use then
with the result to execute the next step. You'll also note that the demo above uses ES6 arrow functions.
This is the hardest thing I've ever had to write, much less admit to myself. I've written resignation letters from jobs I've loved, I've ended relationships, I've failed at a host of tasks, and let myself down in my life. All of those feelings were very...
The <canvas>
element has been a revelation for the visual experts among our ranks. Canvas provides the means for incredible and efficient animations with the added bonus of no Flash; these developers can flash their awesome JavaScript skills instead. Here are nine unbelievable canvas demos that...
When each new t-shirt means staving off laundry for yet another day, swag quickly becomes the most coveted perk at any tech company. Mozilla WebDev had pretty much everything going for it: brilliant people, interesting problems, awesome office. Everything except a t-shirt.
That had to change.
The basic...
Since we've already figured out how to create TinyURL URLs remotely using PHP, we may as well create a small AJAX-enabled tiny URL creator. Using MooTools to do so is almost too easy.
The XHTML (Form)
We need an input box where the user will enter...
This is not a “sleep” function, as it doesn’t block the current (main) thread, it’s more an “after” function; à la Golang Sleep vs. After: https://golang.org/pkg/time/
(of course, reading from the channel in Golang would block, so not really an appropriate comparison either, but you get the idea: things can happen between the call to the function and the “callback” happens)
Of course, nothing like the classic ‘sleep’ function exists in Javascript – and should never exist, actually. The only blocking thing in Javascript has been a sync AJAX request – a relic of the past.
It’s just a way to introduce a similar concept, but still in Javascript’s way of coding.
With
await
, this would look even more sweet,Yes, I was thinking of the same thing. The only thing that bugs me is that every function that uses await should be declared async.
(Actually, it’s the main complaint about async/await…)
This signal callback function I always used my program, sleep await used in synchronus programe
Make sure you clear the timeout when the Promise resolves.
Not a big fan of this approach.
Without
async
/await
, wrapping asetTimeout
call into a non-blockingPromise
does not add any value but readability, not to mention that it prevents the timer from being canceled.I agree that this isn’t a solution for a possible sleep function.
From my point of view it should stop the current process (but not the event handling) and continue (within the same scope when it was called) afterwards.
Thus, you may call it in a function without the need to split the function into two, which is necessary using the approach above.
And when already talking about a sleep function, a
processMessages()
function, similar as in Delphi/Lazarus (Application.processMessages()
) is needed too… :)Not everyone likes or knows (like me) the magic of arrow functions. Here is the “same” code without =>
Ah, and this is usage without arrow function for both:
If you also need to clear promised timeout (like setTimeout does), consider this package: https://github.com/vitalets/await-timeout