Javascript Jquery:如何睡眠或延迟?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2939980/
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: how to sleep or delay?
提问by Koerr
i want move up the object, delay 1000ms , then hide it,
我想向上移动对象,延迟 1000 毫秒,然后隐藏它,
i get the code:
我得到了代码:
$("#test").animate({"top":"-=80px"},1500)
.animate({"top":"-=0px"},1000)
.animate({"opacity":"0"},500);
i use ".animate({"top":"-=0px"},1000)" to implement delay, it's not good.
我用 ".animate({"top":"-=0px"},1000)" 来实现延迟,这不好。
i want:
我想要:
$("#test").animate({"top":"-=80px"},1500)
.sleep(1000)
.animate({"opacity":"0"},500);
any idea?
任何的想法?
回答by Robert Harvey
How about .delay()?
怎么样.delay()?
$("#test").animate({"top":"-=80px"},1500)
.delay(1000)
.animate({"opacity":"0"},500);
回答by mqchen
If you can't use the delaymethod as Robert Harvey suggested, you can use setTimeout.
如果您不能使用delayRobert Harvey 建议的方法,则可以使用setTimeout.
Eg.
例如。
setTimeout(function() {$("#test").animate({"top":"-=80px"})} , 1500); // delays 1.5 sec
setTimeout(function() {$("#test").animate({"opacity":"0"})} , 1500 + 1000); // delays 1 sec after the previous one

![Javascript JS: [Deprecation] 主线程上的同步 XMLHttpRequest 被弃用,因为它对最终用户的体验有不利影响](/res/img/loading.gif)