对jQuery的2行进行排序

对jQuery的2行进行排序,jquery,jquery-ui,animation,sequencing,Jquery,Jquery Ui,Animation,Sequencing,我有以下几行jQuery: // When dragging ends stop: function(event, ui) { // Replace the placeholder with the original $placeholder.after( $this.show() ).remove(); // Run a custom stop function specitifed in the settings settings.stop.apply(thi

我有以下几行jQuery:

// When dragging ends
stop: function(event, ui) {
    // Replace the placeholder with the original
    $placeholder.after( $this.show() ).remove();
    // Run a custom stop function specitifed in the settings
    settings.stop.apply(this);
},
我不需要
设置。停止。应用(此)
运行,直到上面的行是(
$placeholder.after($this.show()).remove();
),现在发生的是
设置。stop
正在提前运行


使用jQuery,我如何排序这两行直到第一行完成才继续?

动画是异步发生的,这就是为什么
$this.show()
设置。停止。应用…
行之前没有完成。所有动画都会在默认(“fx”)队列中结束,并一个接一个地播放。您可以使用将某些内容(即使它不是动画)添加到此序列中。因此,为了适应您的示例:

// When dragging ends
stop: function(event, ui) {
    // Replace the placeholder with the original
    $placeholder.after( $this.show() ).remove();
    // Run a custom stop function specitifed in the settings
    var x = this;   //make sure we're talking about the right "this"
    $this.queue(function() {
        settings.stop.apply(x);
        $(this).dequeue();    //ensure anything else in the queue keeps working
    });
},

编辑回应您的评论“您所说的“这”是什么意思?”:

在JavaScript中,这取决于引用它的范围。在传递给
队列
函数的回调中,
将引用执行
队列
的DOM对象(即,
$this
引用的DOM对象。但是,外部
stop
函数中的
this
完全可能引用其他对象

现在,在您的示例中,外部的
this
引用了由
$this
jQuery对象表示的DOM对象(即,您可能有一个
var$this=$(this);
,在这个代码段的上方某处)。在这种情况下,
x
是不必要的,因为这两个
应该是相同的。但是因为我不知道,我想我应该确定。因此,我*通过创建一个新变量,
x
,它引用了“正确的”
这个
x
现在被捕获在闭包中,因此我们可以确定它指的是
队列中正确的东西
回调)


*这有点费劲,但如果你能看完最后一篇链接文章,你最终会对javascript是如何结合在一起的有一个很好的理解。

另一种等待动画结束的方法是使用它的回调:

stop: function(event, ui) {
     $placeholder.after( $(this).show('fast', 
                           function() { // called when show is done
                              $placeholder.remove(); // var still accessable
                              settings.stop.apply(this);
                            });
                       );

这很好,但让动画很紧张。有什么想法吗?