javascript – 动态创建动画完成时的回调

我有以下代码,我在队列中的两个循环中添加效果:

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动画完成时,它应该触发警报,但我认为我做错了,因为一旦我点击运行按钮就会弹出警报.

我该怎么做呢?

最佳答案 我使用jQuery.Deferred()来使它工作.通过这样做,您可以排队一些延迟对象,这些对象在相应的项目动画完成后解析.

简而言之,创建一个延迟对象数组并使用有点奇怪的构造jQuery.when.apply(…).done(function(){…})等待它们.

看看this JSFiddle.

点赞