How do I wait 1 second before executing next function?
For example like php h开发者_如何学Cas sleep()
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
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.
You can use setTimeOut method to do so http://www.w3schools.com/js/js_timing.asp
setTimeout("alertMsg()",1000);
function alertMsg() { alert("Hello"); }
精彩评论