views:

687

answers:

4

What's the equivalent of Java's Thread.sleep() in Javascript?

A: 

You can either write a spin loop (a loop that just loops for a long period of time performing some sort of computation to delay the function) or use:

setTimeout("Func1()", 3000);

This will call 'Func1()' after 3 seconds.

Edit:

Credit goes to the commenters, but you can pass anonymous functions to setTimeout.

setTimeout(function() {
   //Do some stuff here
}, 3000);

This is much more efficient and does not invoke javascript's eval function.

Malaxeur
I want the current thread to go for waiting state for specified number of seconds
Niger
Spinning a loop cause High CPU utilization.
Niger
I'd prefer to provide the function directly as an argument instead of quoting the function name: setTimeout( function() { ... }, 3000 );
tvanfosson
You should *never* quote the first parameter for setTimeout—pass an anonymous function or a function reference. The corrected version is: setTimeout(Func1, 3000);
Steve Harrison
(Quoting the first parameter of setTimeout invokes "eval()" unnecessarily.)
Steve Harrison
@Steve, if you want to pass any parameters to the method, you really don't have that option, you must quote.
Nick Craver
What's the downside of quoting the first parameter? It essentially executes whatever's in that block... is it a matter of style or are there other considerations?
Malaxeur
@Nick: No, if you want to pass parameters, you use a closure.
Daniel Pryden
Ah, nevermind, missed your later comment. Thanks Steve.
Malaxeur
@Nick: Daniel is correct—you wrap the code in an anonymous function.
Steve Harrison
+1  A: 

There's no direct equivalent, as it'd pause a webpage. However there is a setTimeout(), e.g.:

function doSomething() {
  thing = thing + 1;
  setTimeout(doSomething, 500);
}

Closure example (thanks Daniel):

function doSomething(val) {
  thing = thing + 1;
  setTimeout(function() { doSomething(val) }, 500);
}

The second argument is milliseconds before firing, you can use this for time events or waiting before performing an operation.

Edit: Updated based on comments for a cleaner result.

Nick Craver
Again, *never* quote the first parameter of setTimeout. See my comments on Malaxeur's answer for more info.
Steve Harrison
+13  A: 

The simple answer is that there is no such function.

The closest thing you have is:

var millisecondsToWait = 500;
setTimeout(function() {
    // Whatever you want to do after the wait
}, millisecondsToWait);

Note that you especially don't want to busy-wait (e.g. in a spin loop), since your browser is almost certainly executing your JavaScript in a single-threaded environment.

Here are a couple of other SO questions that deal with threads in JavaScript:

And this question may also be helpful:

Daniel Pryden
(+1) look at setTimeout() and setInterval() in javascript
Mahesh Velaga
To promote good coding practices, it might be best to insert a semi-colon after "500" and initialise "millisecondsToWait" in the code sample (e.g. by preceding it with "var ") (this way, if someone copies and pastes the sample, they won't end up with an implied global).
Steve Harrison
Good catch, Steve. I've edited my answer to reflect your comments.
Daniel Pryden
A: 

Or maybe you can use the setInterval function, to call a particular function, after the specified number of milliseconds. Just do a google for the setInterval prototype.I don't quite recollect it.

The Machine