wait
wait

Reputation: 21

Sleep in javascript (or equivalent)

How do I wait 1 second before executing next function?

For example like php has sleep()

Upvotes: 0

Views: 6892

Answers (3)

George P
George P

Reputation: 736

Even though setTimeout is supported in all major browsers, I prefer to use a javascript library because usually one is doing more js than just calling a timeout function. In YUI its:

YAHOO.lang.later(1000, this, function() {
...
});

More information here.

Upvotes: 1

Prashant Singh
Prashant Singh

Reputation: 3793

You can use setTimeOut method to do so http://www.w3schools.com/js/js_timing.asp

setTimeout("alertMsg()",1000);

function alertMsg() { alert("Hello"); }

Upvotes: 0

Raynos
Raynos

Reputation: 169421

setTimeout(f, 1000);

Set a timeout to run a function after 1000 milliseconds

window.setTimeout[docs]

window.setTimeout[spec]

window.setTimeout[dark side]

As mentioned in the comments. JavaScript is single threaded and that thread is shared with the browser UI thread.

So by calling a blocking function like sleep. You would block the only thread. This also means that the client cannot interact with the page whilst it is blocking.

See the Event Driven Programming[wiki] article for more information

Upvotes: 5

Related Questions