I still can't figure out what use a sleep function for JS is.

Okay, here is why I need it: I am programming a multi-player game based on the old "concentration" game where you flip over two cards and if they match, you get a point. Otherwise play moves to the next player.
Now this work's great when you're playing it on a table and you see what cards the other player flipped, but when you're playing online and are at a different computer, I want to reveal the two cards that were flipped to the remote players, and I think it is more pleasing (and realistic) to flip one card over, wait a second, then flip the second one.
Okay, sure, I can certainly work around this with timeouts, etc. But really all I want is a simple sleep function so I can do this:
revealCard (card1);
sleep (1000);
revealCard (card2);
sleep (1000);
clearCards ();
// I would prefer to get control back here after cards are cleared.
- revealCard (card1);
- sleep (1000);
- revealCard (card2);
- sleep (1000);
- clearCards ();
-
- // I would prefer to get control back here after cards are cleared.
Instead, my solution is:
revealCard (card1);
setTimeout("revealCard ("+card2+")", 1000);
setTimeout("clearCards()", 2000);
setTimeout("restOfCode("+var1+","+var2+",...,"+varN+")", 3000);
- revealCard (card1);
- setTimeout("revealCard ("+card2+")", 1000);
- setTimeout("clearCards()", 2000);
- setTimeout("restOfCode("+var1+","+var2+",...,"+varN+")", 3000);
where restOfCode is the code I want to execute after clearing cards, and var1-varN are the local variables I now want to pass to this code.
We can debate which is more elegant, but I personally think the sleep version is more straightforward.
...Lance