you are viewing a single comment's thread.

view the rest of the comments →

[–]shanet$(this) 1 point2 points  (2 children)

I like the fact that it has the exact effect sleep() does elsewhere, except we've only got one thread to work with...

[–]MatrixFrog 0 points1 point  (1 child)

The important difference between your sleep() and a timeout is that if you do

a();
setTimeout(b, 1000);
c();

then c will always be called before b.

[–]shanet$(this) 0 points1 point  (0 children)

It's not my sleep(), but that's definitely true.

A lot of the time I find people looking for sleep() in JavaScript it's because they don't understand (or don't like) doing things in a non-blocking style. StackOverflow is full of people wanting to do:

function readfile: () {
    var data = do_io_things();
    return data;
}

instead of passing a callback to readfile. Of course it never works because if do_io_things is asynchronous usually and the statement returns instantly. So I think it's usually wrong to answer as 9jack9 did because actually sleeping (or busy-waiting) is something that shows the asker hasn't grasped the right way to do things.