Javascript jquery动画-何时完成?

Javascript jquery动画-何时完成?,javascript,jquery,jquery-ui,Javascript,Jquery,Jquery Ui,我有两个jquery动画,一个接一个: books.animate({ left: left1 }, { duration: this.slideDuration, easing: this.easing, complete: complete }); laptops.animate({ left: left2 }, { duration: this.

我有两个jquery动画,一个接一个:

        books.animate({ left: left1 }, {
            duration: this.slideDuration,
            easing: this.easing,
            complete: complete
        });

        laptops.animate({ left: left2 }, {
            duration: this.slideDuration,
            easing: this.easing,
            complete: complete
        });
我希望动画同时运行,因此我使用
{queue:false}

books.animate({ left: left1 }, {
            duration: this.slideDuration,
            easing: this.easing,
                            queue: false,
            complete: complete
        });

        laptops.animate({ left: left2 }, {
            duration: this.slideDuration,
            easing: this.easing,
                            queue: false,
            complete: complete
        });

但是现在完成的回调调用了两次!我如何确切地知道这两个动画何时完成?

为什么不从其中一个动画中删除
complete
处理程序


从您发布的代码摘录来看,似乎您在两个动画上使用了相同的持续时间和缓和方法。因此,只要在同一时间调用它们,它们将在同一时间完成,这一点在本质上是正确的。

这听起来可能有些复杂,但为什么不
延迟对象呢

你可以在这里进行更多的调查

使用jQuery延迟方法尝试

$.when( 
    books.animate({ left: left1 }, {
        duration: this.slideDuration,
        easing: this.easing
    }),
    laptops.animate({ left: left2 }, {
        duration: this.slideDuration,
        easing: this.easing
  })
).done( function( ) {

    alert("done!");

});
根据以下内容演奏小提琴:


我看不出这是怎么回事?jQuery如何理解匿名函数中的动画何时完成?@NULL您完全正确。谢谢你。我已经更正了我的代码。@NULL:看来animate返回了延迟的对象,可以与when一起使用。我工作得很好。在这种情况下,我真的可以指望持续时间,但如果明天有人更改持续时间,代码将无法正常工作。假设有两个ajax调用,我们需要知道它们何时完成。
books.animate({ left: left1 }, {
    duration: this.slideDuration,
    easing: this.easing,
    queue: false
});

laptops.animate({ left: left2 }, {
    duration: this.slideDuration,
    easing: this.easing,
    queue: false
});

$.when(books, laptops).done(complete);