我读过很多关于这个主题的问题。但没有人有我的问题。 实际上我的主代码中有这个:
// getHtmlInfoWindows has inside an ajax request
var infowindowString = getHtmlInfoWindow(selectedTrucks[i], true);
makeInfoWindowEvent(map, infowindow[i], infowindowString, markers[i], "click");
这里是 getHtmlInfoWindows() 代码
$.ajax({
url: ajaxUrl,
})
.done(function(data) {
// some line of code and operation
return someData;
}
基本上,我会等到 getHtmlInfoWindow() 完成,然后执行 makeInfoWindowEvent() 但我不知道如何执行。
我已经尝试过这个:
$.when(infowindowString = getHtmlInfoWindow(selectedTrucks[i], true)).done(function(){}
但不起作用,因为必须返回感兴趣的ajax响应,而不是我想要的单个值“someData”。 我能怎么做?
谢谢大家
请您参考如下方法:
将您想要执行的代码作为回调函数传递:
// getHtmlInfoWindows has inside an ajax request
var callback = function(data){
makeInfoWindowEvent(map, infowindow[i], data, markers[i], "click");
}
getHtmlInfoWindow(selectedTrucks[i], true, callback);
然后在 ajax 完成时调用回调:
$.ajax({
url: ajaxUrl,
})
.done(function(data) {
// some line of code and operation
callback(data);
}