我有下面的样品。我总是把"c“放在"a”和"b“之前。如何相应地得到"a“、"b”和"c“?如有任何建议,我将不胜感激。
b.extend({
get: function (id) {
jQuery.ajax({
type: 'GET',
url: url,
data: pdata,
success: function (result) {
console.log("a");
}
});
for (var a = 0; a < 5; a++) {
jQuery.ajax({
type: 'GET',
url: url,
data: pdata,
success: function (result) {
console.log("b");
}
});
}
console.log("c");
}
});发布于 2013-11-19 14:11:17
您也可以使用递延
b.extend({
get: function (id) {
var request = jQuery.ajax({
type: 'GET',
url: url,
data: pdata
}).then(function(result) {
console.log("a");
return result;
});
for (var a = 0; a < 5; a++) {
request = request.then(function(result) {
return jQuery.ajax({
type: 'GET',
url: url,
data: pdata
}).then(function(result) {
console.log("b");
return result;
});
});
}
request.then(function() {
console.log("c");
});
}
});发布于 2013-11-19 14:03:30
试一试
将您的代码放在success:中
b.extend({
get: function (id) {
jQuery.ajax({
type: 'GET',
url: url,
data: pdata,
success: function (result) {
console.log("a");
for (var a = 0; a < 5; a++) {
jQuery.ajax({
type: 'GET',
url: url,
data: pdata,
success: function (result) {
console.log("b");
if (a === 5) {
console.log("c");
}
}
});
}
}
});
}
});发布于 2013-11-19 14:03:34
在回调B中调用C,在回调A中调用B
https://stackoverflow.com/questions/20073557
复制相似问题