步骤:
在处理数据时,我需要暂停执行,直到我从第二次Web服务调用中获取图像。我尝试使用回调但无法实现。
下面的是代码段
//get roster data from web service:1
function getRosterData(callback) {
var rosterToolURL; //web service url
var xhrRosterTools = Ti.Network.createHTTPClient({
onload : function() {
var rosterJsonData = JSON.parse(this.responseText);
//pass response to callback
callback(rosterJsonData);
},
onerror : function(e) {
Ti.API.info("STATUS: " + this.status);
},
});
xhrRosterTools.open("GET", rosterToolURL);
xhrRosterTools.send();
}
//process the roster data and call getUserImage for each row
function rosterWIndow(rosterJsonData) {
var roster_collection = rosterJsonData.roster_collection;
for ( i = 0; i < roster_collection.length; i++) {
var rosterInfo = roster_collection[i];
var imageURL = rosterInfo.imageUrl;
//call get user
getUserImage(imageURL, function(imageData) {
Ti.API.info('after call back2' + imageData);
});
}
}
//get image from web service:2
function getUserImage(imageURL) {
var xhrinfo = Ti.Network.createHTTPClient({
onload : function() {
Ti.API.info('response data' + this.responseData);
return this.responseData;
},
onerror : function(e) {
Ti.API.info("STATUS: " + this.status);
},
});
xhrinfo.open("GET", imageURL);
xhrinfo.send();
}
getRosterData(rosterWIndow);
&#13;