您好我一直在努力做到以下几点 我正在设计的系统中有一个模块,如下所示
var queryDispatcher = {
init: function () {
var g = new Array() ;
var b = new Array() ;
var y = new Array() ;
var total_google = 0 ;
var total_bing = 0 ;
var total_yahoo = 0 ;
return {
callGoogle: function (query) {
var url = 'http://localhost/meta/public/google/'+query ;
$.getJSON(url, function(data) {
total_google = data.searchInformation.totalResults;
var i = 0 ;
$.each(data.items, function() {
var obj = new res(i+1, this.title, this.snippet, this.link, 0) ;
g.push(obj) ;
i=i+1 ;
});
});
return true ;
},
callBing: function (query) {
var url = 'http://localhost/meta/public/bing/'+query ;
$.getJSON(url, function(data) {
total_bing = data.d.results[0].WebTotal;
var j = 0 ;
$.each(data.d.results[0].Web, function() {
var obj = new res(j+1, this.Title, this.Description, this.Url, 0) ;
b.push(obj) ;
j=j+1 ;
});
});
},
callYahoo: function (query) {
var url = 'http://localhost/meta/public/yahoo/'+query ;
$.getJSON(url, function(data) {
total_yahoo = data.bossresponse.web.totalresults;
var k = 0 ;
$.each(data.bossresponse.web.results, function() {
var obj = new res(k+1, this.title, this.abstract, this.url, 0) ;
y.push(obj) ;
k=k+1 ;
});
});
},
getGoogle: function () {
}
}
}
};
现在,如果我实例化queryDispatcher,如下所示:
var qd = queryDispatcher.init();
var google = qd.callGoogle("hey there");
我应该能够检索结果。目前结果被推入数组g,它最初为空,并且由于异步getJSON调用,当我尝试返回g时,它总是为空。
我如何等待异步调用完成,以便我可以返回数组g和total_google变量?