4

キュー内の2つのループからのエフェクトを追加する次のコードがあります。

tablaActual = ({
    1111: {
        titulo: "Nuevo Video 1",
        usuario: "RadioBot",
        alta: "1353182478"
    },
    2243: {
        titulo: "Old Boy Fashion",
        usuario: "RadioBot",
        alta: "1353182479"
    },
    3432: {
        titulo: "Britney spears",
        usuario: "RadioBot",
        alta: "1353182825"
    }
});

tablaNueva = ({
    1111: {
        titulo: "Nuevo Video 1",
        usuario: "RadioBot",
        alta: "1353182478"
    },
    1112: {
        titulo: "Nuevo video 2",
        usuario: "RadioBot",
        alta: "1353182477"
    },
    1113: {
        titulo: "Nuevo video 3",
        usuario: "RadioBot",
        alta: "1353182476"
    }
});


$("button").bind("click", function() {

    var body = $('body');

    retardation = 500;
    i = 1;

    // we delete the old ones that doesnt match in the new table
    $.each(tablaActual, function(index) {
        if (!tablaNueva[index]) {
            delay = i * retardation;

            $('#lista #id' + index).delay(delay).slideUp("normal", function() {
                $(this).remove();
            }).queue("cola1");

            delete tablaActual[index];
            i++;
        }
    });

    // we add the new ones that doesnt match in the old table
    $.each(tablaNueva, function(index, vars) {
        if (!tablaActual[index]) {

            delay = i * retardation;
            $('<tr id="id' + index + '"><td class="cancion"><h3>' + vars.titulo + '</h3></td><td class="autor">' + vars.usuario + '<span>' + vars.alta + '</span></td></tr>').appendTo('#lista').hide().delay(delay).show('slow').queue("cola2");


            tablaActual[index] = vars;
            i++;


        }
    });

    $("tr:animated").promise().done(function() {

        alert("done");
    });


});

jsFiddle

すべてのTRアニメーションが終了すると、アラートがトリガーされるはずですが、実行ボタンをクリックするとすぐにアラートがポップアップするため、間違っていると思います。

どうすればよいですか?

4

2 に答える 2

2

jQuery.Deferred()を使用して機能させます。これを行うことにより、対応するアイテムのアニメーションが終了すると解決するいくつかの遅延オブジェクトをキューに入れます。

要するに、据え置きオブジェクトの配列を作成し、やや奇妙な構成を使用してそれらを待ちますjQuery.when.apply(...).done(function() { ... })

このJSFiddleを見てください。

于 2012-11-18T15:44:43.503 に答える
0

関数のコールバックでアラート(コールバックの終了)を移動し、showすべてのアニメーションが完了したかどうかをテストできます:http: //jsfiddle.net/dSGWx/12/

var totalmacth=0;
var loop=0;
// los que se tienen que añadir
$.each(tablaNueva, function(index, vars) {
    if (!tablaActual[index]) {
    totalmacth++;

    delay = i * retardation;
    $('<tr id="id' + index + '"><td class="cancion"><h3>' + vars.titulo + '</h3></td><td class="autor">' + vars.usuario + '<span>' + vars.alta + '</span></td></tr>').appendTo('#lista').hide().delay(delay)
         .show('slow',function(){
              loop++; 
              console.log(loop + ' ' + totalmacth)                
              if(loop === totalmacth)
                  alert("done");

          });
    tablaActual[index] = vars;
    i++;
    }      
});
于 2012-11-18T15:49:16.913 に答える