Javascript jQuery睡眠功能?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3643562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery sleep function?
提问by Matt Elhotiby
Possible Duplicate:
Jquery: how to sleep or delay?
可能的重复:
Jquery:如何睡眠或延迟?
var main = $('#main_content');
main.append("<img id=\"throbber\" src='/pre_config/css/images/throbber.gif' alt='Loading. Please wait.' />");
sleep(3000);
$("#main_content").load("/pre_config/function.php?type="+type+"piz&count=1", successCallback );
I want to sleep for 3 seconds here
我想在这里睡3秒钟
sleep is not defined
[Break on this error] sleep(3000);
func2
回答by user113716
Use a javascript setTimeout().
使用 javascript setTimeout()。
setTimeout(function() {
$("#main_content").load("/pre_config/function.php?type="+type+"piz&count=1", successCallback );
}, 3000);
EDIT:
编辑:
To use .delay(), you would need to add the delayed code to an animation queue.
要使用.delay(),您需要将延迟代码添加到动画队列中。
$("#main_content")
.delay(3000)
.queue(function( n ) {
$(this).load("/pre_config/function.php?type="+type+"piz&count=1", successCallback )
.dequeue();
});
回答by Daniel Vassallo
Keep in mind that JavaScript is not only single threaded, but it shares the same thread with the page rendering. Therefore there are no in-built blocking functions in JavaScript, because that would make the page UI unresponsive.
请记住,JavaScript 不仅是单线程的,而且与页面渲染共享同一个线程。因此 JavaScript 中没有内置的阻塞函数,因为这会使页面 UI 无响应。
You should consider using timers instead, using setTimeout(), setInterval(), or jQuery's delay(). Check out @patrick's answerfor a couple of examples.
你应该考虑使用定时器来代替,使用setTimeout(),setInterval()或jQuery的delay()。查看@patrick 的答案以获取几个示例。

