代码如下:
function getPrdInfo(index,reqUrl){
var defer = $.Deferred();
$.reqWeb({
url:reqUrl,
dataType:"html",
async:true,
beforeSend:function(){
$('.con_switch'+index).hide();
$(".plant_loading").show();
},
success:function(data){
$('.con_switch'+index).html(data);
//此行代码就是把ajax返回的结果保存在defer对象中
defer.resolve(data);
},
complete:function(){
$('.con_switch'+index).show().siblings().hide();
$(".plant_loading").hide();
//异步加载 给最后加载完的span添加高亮
$(".con_switch span:eq("+(index-1)+")").addClass('con_active').siblings().removeClass('con_active');
}
});
return defer.promise();
}
//此方法的作用就是等getPrdInfo方法结束后,获取其返回结果,供其它地方使用
function callbackFn(index,reqUrl){
$.when(getPrdInfo(index,reqUrl)).done(function(data){
console.log(data);
//其它操作
})
}