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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:35:45  来源:igfitidea点击:

Jquery: how to sleep or delay?

javascriptjquery

提问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()

http://api.jquery.com/delay/

http://api.jquery.com/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