当前位置 : 主页 > 网络编程 > JavaScript >

$.when().done()的用法.js

来源:互联网 收集:自由互联 发布时间:2021-06-30
$.when().done()的用法.js $.ajax({ url: "/home/GetProduct", dataType: "JSON", type: "GET", success: function (data) { $.ajax({ url: "/home/GetProduct", dataType: "JSON", type: "GET", success: function (data) { $.ajax({ url: "/home/GetProd
$.when().done()的用法.js
$.ajax({
    url: "/home/GetProduct",
    dataType: "JSON",
    type: "GET",
    success: function (data) {
        $.ajax({
            url: "/home/GetProduct",
            dataType: "JSON",
            type: "GET",
            success: function (data) {
                $.ajax({
                    url: "/home/GetProduct",
                    dataType: "JSON",
                    type: "GET",
                    success: function (data) {
                        console.log(0)
                    }
                })
            }
        })
    }
})


$.when($.ajax({
    url: "/home/GetProduct",
    dataType: "JSON",
    type: "GET",
    success: function (data) {
        alert(JSON.stringify(data));
    }
})).done(function (data) {
    alert(data[0].Name);
}).done(function (data) {
    alert(data[1].Name);
}).fail(function () {
    alert("程序出现错误!");
}).then(function (data) {
    alert("程序执行完成");
});


var log = function(msg){
    window.console && console.log(msg)
}
function asyncThing1(){
    var dfd = $.Deferred();
    setTimeout(function(){
        log('asyncThing1 seems to be done...');
        dfd.resolve('1111');
    },1000);
    return dfd.promise();
}
function asyncThing2(){
    var dfd = $.Deferred();
    setTimeout(function(){
        log('asyncThing2 seems to be done...');
        dfd.resolve('222');
    },1500);
    return dfd.promise();
}
function asyncThing3(){
    var dfd = $.Deferred();
    setTimeout(function(){
        log('asyncThing3 seems to be done...');
        dfd.resolve('333');
    },2000);
    return dfd.promise();
}
/* do it */
$.when( asyncThing1(), asyncThing2(), asyncThing3() ).done(function(res1, res2, res3){
    log('all done!');
    log(res1 + ', ' + res2 + ', ' + res3);
})
上一篇:transform.html
下一篇:JQ JSON操作类
网友评论