Javascript 如何分组2 jQuery动画';什么是完全的回调?

Javascript 如何分组2 jQuery动画';什么是完全的回调?,javascript,jquery,Javascript,Jquery,My函数有不同的执行路径,其中可以调用一个或多个.animatejQuery方法。这些动画应该同时开始,不应该序列化。例如: function moveDown() { busy = true; if(some condition) { $(".selFrame").animate({top: $("#row" + currentSelRowIndex).offset().top}); } else { $(".co

My函数有不同的执行路径,其中可以调用一个或多个
.animate
jQuery方法。这些动画应该同时开始,不应该序列化。例如:

function moveDown()
{
    busy = true;
    if(some condition)
    {
        $(".selFrame").animate({top: $("#row" + currentSelRowIndex).offset().top});
    }
    else
    {
        $(".container").animate({scrollTop: $("#row" + currentRowIndex).offset().top - $("#row0").offset().top});
        $(".selFrame").animate({top: ($(".selFrame").offset().top + ch - remaining)});
    }
}
我的目标不是使用一些
busy
变量来检测是否所有动画都已完成,无论我们通过哪个执行路径。对于一个动画调用,可以轻松重置动画上的
busy
变量
complete
。但是,我不知道如何将两个或多个动画组合到一个事件中,在该事件中我可以重置
busy
变量,以确保所有动画都已完成

更新

不幸的是,经过彻底测试后,过滤溶液并没有给出良好的结果

document.addEventListener('keydown', function(e) {
    var runningAnimations = $(".container, .selFrame").filter(":animated").length;
    if(runningAnimations == 0)
    {
        switch(e.keyCode)
        {
        case 40: 
            moveDown();
            break;
它比没有更好,但有时,在允许重新进入的双动画情况下


我将使用
promise

尝试第二种方法。您可以使用一个整数保存动画的数量,并在每个动画的回调中减去一个,或者检查其值是否为1:

    if(some condition)
    {
        busy = 1;
        $(".selFrame").animate({top: $("#row" + currentSelRowIndex).offset().top} , function(){
             busy--;
        });
    }
    else
    {
        busy = 2;
        $(".container").animate({scrollTop: $("#row" + currentRowIndex).offset().top - $("#row0").offset().top}, function(){
             if(busy == 1) // all the animations have ended
             busy--;
        });

        $(".selFrame").animate({top: ($(".selFrame").offset().top + ch - remaining), function(){
             if(busy == 1) // all the animations have ended
             busy--;
        }});
    }
您还可以将
:animated
选择器与
设置超时一起使用:

setTimeOut(function(){
    var runningAnimations = $( ".container, .selFrame" ).filter( ":animated" ).length;
   }, 500);

如果希望在与元素集合相关的所有动画完成时收到通知,可以使用:


例如,这可能发生在您的
if/else

之后。对不起,
setTimeOut
与第二个解决方案的关系如何?我已经更新了答案,添加了
setTimeOut
实现。我现在看到了。每当我需要根据是否有活动动画来决定是否可以运行例程时,我可以使用
runningAnimations
而不超时。似乎我甚至不需要
setTimeOut
。只要有什么东西触发检查,因为动画是异步运行的,这就是使用
setTimeOut
这个方法,与
filter
相比,这个方法被证明更稳定。
$(".container, .selFrame").promise().then(function(){
      // all finished
      // we may change a boolean but usually we would call a callback
});