如何使用$.when().done()实现异步操作成功回调?

2026-04-06 11:131阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计215个文字,预计阅读时间需要1分钟。

如何使用$.when().done()实现异步操作成功回调?

javascript$.when().done(function() { $.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()实现异步操作成功回调?

$.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); })

本文共计215个文字,预计阅读时间需要1分钟。

如何使用$.when().done()实现异步操作成功回调?

javascript$.when().done(function() { $.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()实现异步操作成功回调?

$.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); })