解决方案
把ajax改为异步,用deferred
代码
function renderChildData(d) { var params = '&' + d.hasNewChild + '&baseTime=' + getparams().baseTime + '&period=' + getparams().period; var childTreeData = null; var deferred = $.Deferred(); var request = $.ajax({ url: api.childTree + params,type: 'GET',data: {},beforeSend: function () { $('#childLoading').html(common.loadingImg); },dataType: 'json' }); request.done(function (json) { $('#childLoading').empty(); if (json.success) { var data = json.data; childTreeData = mapTreeData(data); deferred.resolve(childTreeData); } else { nuit.msg({type: 'warn',msg: json.msg,closable: true,timeoutclose: true}); deferred.reject(childTreeData); } return childTreeData; }); request.fail(function () { $('#childLoading').empty(); deferred.reject(childTreeData); }); return deferred; } renderChildData(d).then(function (child) { // 这里的child 就是renderChildData 返回的childTreeData }原文链接:https://www.f2er.com/ajax/163607.html