var dtd = $.Deferred(); // 新建一个Deferred对象
var wait = function(dtd){
var tasks = function(){
alert("执行完毕!");
dtd.reject(); // 改变Deferred对象的执行状态
};
setTimeout(tasks,);
return dtd;
};
$.when(wait(dtd))
.done(function(){ alert("哈哈,成功了!"); })
.fail(function(){ alert("出错啦!"); });
$ajax: function (url, type, data, exParams) {
//$ajax: function (url, type, data, success, error, exParams) {
var dtf = $.Deferred();
var ps = {
url: url,
type: type,
dataType: 'JSON',
contentType: 'application/json; charset=utf-8',
data: data || {},
success: function (d, s, req) {
//success(d, s, req);
dtf.resolve(d);
},
error: function (req, s, e) {
//error(req, s, e);
dtf.reject(req.responseJSON);
}
};
if (exParams && typeof exParams != 'undefined') {
ps = $.extend({}, exParams, ps);
}
if (!ps.url || typeof ps.url == 'undefined') {
setTimeout(function () {
dtf.reject({ Error: '请求地址无效', ErrorCode: - });
}, );
} else {
$.ajax(ps);
}
return dtf.promise();
}
});