0

What technical or other reasons prevent the implementation of sleep() in Javascript?

UPDATE: I'm aware of ways to delay execution of code (setTimeout, setInterval etc.) The problem these create is that the developer is forced to split the code block into two parts, that which is executed before the time-gap, and that which is executed afterwards, which is arguably undesirable.

Athyuttam Eleti
  • 589
  • 5
  • 9
  • 2
    The real question is, why would you ever halt the execution of javascript in the browser, what possible reason would there be to make a user wait for a sleep function ? – adeneo Jul 28 '14 at 20:10
  • If `sleep()` were to prevent the user from interacting with the rest of the page, that would be pretty useless. What about an implementation that basically has the same uses as `setTimeout()`? – Athyuttam Eleti Jul 28 '14 at 20:12
  • `setTimeout(function(){alert('Is it morning already!?');}, 10000);` – Shomz Jul 28 '14 at 20:12
  • 1
    [Every feature starts off at a score of -100](http://stackoverflow.com/a/8673015/16587)... – George Stocker Jul 28 '14 at 20:13
  • This is not an opinion-based question. It has a technical answer. – Ben Aston Jul 28 '14 at 20:15
  • you can sleep() using showModalDialog() or sync ajax with a delayed response... – dandavis Jul 28 '14 at 20:18
  • To emulate a sleep function is easy, it's just an endless while loop, but again, why would you ever want to do that ? – adeneo Jul 28 '14 at 20:30
  • A sleep function pauses execution on a thread for a specified time. JavaScript is single-threaded and so a sleep function doesn't make much sense - it would simply stop execution of *everything* in your program for a specified time. It is trivial to implement your own sleep -like function using `setTimeout(continuationFunction, durationToWaitInMs)`, but note that any code after this statement will continue to execute immediately. You cannot stop the main event-loop. – Ben Aston Jul 28 '14 at 20:36

1 Answers1

2

It does. It's called setTimeout().

The common JavaScript environments (the browser and node.js) are single threaded and event driven. They need to keep servicing their main event loop. Just flat out stopping the thread will stop the background processes as well, since it depends on the event loop firing.

This is common in just about all user-facing environments; Windows, Mac OS, Java swing, Android etc. all require you to keep the main UI thread running; you can't just sleep() on it.

Chris Tavares
  • 29,165
  • 4
  • 46
  • 63
  • Fair enough, stopping the entire thread is counter-productive. The problem with setTimeout is that if all you're trying to achieve is a momentary pause in your function, then you need to pretty much break it up into two separate blocks of code. Would "sleeping" the current function's execution alone for some time be safe? – Athyuttam Eleti Jul 28 '14 at 20:18
  • In general, no, it's not. My question is why do you want a momentary pause? There's generally two reasons: 1) you're waiting for something else to finish, or 2) you want to control timing (a game loop or something). In the first case, if you block the UI thread then you won't even know if the other thing finished, since it will have to run on the UI thread, which is blocked. In the second case, you're much better off using explicit timing functions. So no, in general it's not safe. Accept the complexity of what you're trying to do. – Chris Tavares Jul 28 '14 at 20:29
  • the "problem" of breaking a code span into two pieces is a very minor problem compared to what results from freezing processing. since js has closure, you can often just wrap the 2nd part in an anon and not have to breakup anything... – dandavis Jul 28 '14 at 20:30
  • @AthyuttamReddy What you probably want to do is delay completion of the evaluation of a function. To achieve this, you don't need a sleep function, simply place the code you want delayed inside a `setTimeout` call. – Ben Aston Jul 28 '14 at 20:38
  • @ChrisTavares - Thank you. I understand the complexity with Javascript being single-threaded now. However, shouldn't there be a way to achieve the effect of setTimeout without breaking up code, in a single line? One way is to write my own synchronous function that returns after a certain time, but that still uses client-side resources, which is unwarranted. That's where my question arises from (not from an unfamiliarity with setTimeout.) – Athyuttam Eleti Jul 28 '14 at 20:56
  • Under the hood, the break up of code is necessary. This can be automatically done by compilers; that's what async/await in C# does, for example. For Javascript take a look at https://github.com/Sage/streamlinejs as one system that does this. – Chris Tavares Jul 28 '14 at 21:00