使用Primose方式解決異步編程回調的一些問題--animate動畫的例子

function animate(dis, time) {

    var def = $.Deferred();

    $('.boll')

    .animate({

        left: dis + 'px'

    },
    time,
    function() {

        def.resolve(time);

    });

    return def;

}

$('.boll').on('click',
function() {

    $.when(

    animate(50, 1000),

    animate(120, 100),

    animate(200, 500))

    .done(function(dd, tt, kk) {

        console.log(dd, tt, kk)

    })

});

  when裏參數必須是Promise對象,when(xxx) 返回的也是一個Promise。javascript

相關文章
相關標籤/搜索