其他分享
首页 > 其他分享> > JQuery多个异步操作后执行(resolve,promise,when,done)

JQuery多个异步操作后执行(resolve,promise,when,done)

作者:互联网

代码分享:

//3秒后完成
function asyncThing1() {
    var dfd = $.Deferred();
    setTimeout(function () {
        alert('asyncThing1 seems to be done...');
        dfd.resolve('111');
    }, 3000);
    return dfd.promise();
}
//5秒后完成
function asyncThing2() {
    var dfd = $.Deferred();
    setTimeout(function () {
        alert('asyncThing2 seems to be done...');
        dfd.resolve('222');
    }, 5000);
    return dfd.promise();
}
//1秒后完成
function asyncThing3() {  
    var dfd = $.Deferred();
    setTimeout(function () {
        alert('asyncThing3 seems to be done...');
        dfd.resolve('333');
    }, 1000);
    return dfd.promise();
}
//全部完成后
$.when(asyncThing1(), asyncThing2(), asyncThing3()).done(function (res1, res2, res3) {
    alert('all done!');
    alert(res1 + ', ' + res2 + ', ' + res3);
})

 

标签:JQuery,function,resolve,when,alert,done,promise,dfd
来源: https://www.cnblogs.com/kandyvip/p/10948902.html