Jquery:如何睡觉或延迟?

Koe*_*err 60 javascript jquery

我想向上移动物体,延迟1000ms,然后隐藏它,

我得到的代码:

$("#test").animate({"top":"-=80px"},1500)
      .animate({"top":"-=0px"},1000)
      .animate({"opacity":"0"},500);
Run Code Online (Sandbox Code Playgroud)

我用".animate({"top":" - = 0px"},1000)"来实现延迟,这不好.

我想要:

$("#test").animate({"top":"-=80px"},1500)
      .sleep(1000)
      .animate({"opacity":"0"},500);
Run Code Online (Sandbox Code Playgroud)

任何的想法?

Rob*_*vey 95

怎么样.delay()

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

$("#test").animate({"top":"-=80px"},1500)
          .delay(1000)
          .animate({"opacity":"0"},500);
Run Code Online (Sandbox Code Playgroud)

  • 这适用于这种情况,但告诉线程睡眠和延迟效果是两个完全不同的事情.jQuery甚至在他们的文档中提醒了这一点. (4认同)
  • 这是1.4的新功能. (3认同)

mqc*_*hen 56

如果您不能delay像Robert Harvey建议的那样使用该方法,则可以使用setTimeout.

例如.

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
Run Code Online (Sandbox Code Playgroud)