如何将ajax请求同步化
作者:互联网
原文链接:http://www.cnblogs.com/LiuXianBlog/p/3579814.html
(function ($) { var a = ['test1', 'test2', 'test3', 'test4']; recursive(3, 'test').done(function (result) { console.log(result); }); function get(id) { var dfd = $.Deferred(); setTimeout(function () { dfd.resolve(a[id]); }, 1000); return dfd.promise(); } function recursive(n, result) { var dfd = $.Deferred(); if (n == 0) { get(n).done(function (result1) { dfd.resolve(result1 + result); }); } else { get(n).done(function (result2) { recursive(n, result2).done(function (result3) { dfd.resolve(result3 + result); }) }) } n--; return dfd.promise(); } })(jQuery)
基于jquery 1.6+
结果:
test1test2test3test4test
转载于:https://www.cnblogs.com/LiuXianBlog/p/3579814.html
标签:function,请求,get,ajax,done,result,同步化,dfd,recursive 来源: https://blog.csdn.net/weixin_30518397/article/details/96614952