正如标题所述,尽管可能有点含糊,但当我调用一个函数返回另一个函数中的对象数组时,由于某种原因,我无法将结果捕获到另一个函数中。当前功能。我觉得我只是想念的一小件事。
这里是返回对象数组的函数:
function GetAllNeighboors(myLocationLat, myLocationLong) {
$.get("/Home/GetAllLocation",
function (data, status) {
Neightbours = new Object();
var j = 0;
for (var i = 0; i < data.length; i++) {
if (distanceBetweenTwoStations(myLocationLat, myLocationLong, data[i].latitudine, data[i].longitudine,"K") <= 1 && data[i].latitudine != myLocationLat && data[i].longitudine != myLocationLong) {
Neightbours[j] = data[i];
j++;
}
}
return Neightbours;
});
}
这是我调用该函数的地方:
if (data.isClosed != true) {
map.setCenter(new google.maps.LatLng(data.latitudine, data.longitudine));
map.setZoom(17);
} else {
Vecini = new Object();
Vecini = GetAllNeighboors(data.latitudine, data.longitudine);
注意:第一个功能的Neightbours
带有预期的结果。
注2:我想我知道问题出在哪里,但我不确定如何解决。问题是我不等待结果,我应该使函数异步,以便在第二个函数中得到结果。知道我该怎么做吗?
答案 0 :(得分:0)
您没有从第一个函数返回任何内容,而是从该函数内部的函数返回了内容。试试这个:
function GetAllNeighboors(myLocationLat, myLocationLong) {
return $.get("/Home/GetAllLocation",
function (data, status) {
Neightbours = new Object();
var j = 0;
for (var i = 0; i < data.length; i++) {
if (distanceBetweenTwoStations(myLocationLat, myLocationLong, data[i].latitudine, data[i].longitudine,"K") <= 1 && data[i].latitudine != myLocationLat && data[i].longitudine != myLocationLong) {
Neightbours[j] = data[i];
j++;
}
}
return Neightbours;
});
}
答案 1 :(得分:0)
您可以将git reset --soft the-other-branch
传递给callback
函数,并像这样在$ .get回调中设置其值
GetAllNeighbours
您现在这样称呼它:
function GetAllNeighboors(myLocationLat, myLocationLong, cb) {
$.get("/Home/GetAllLocation",
function (data, status) {
Neightbours = new Object();
var j = 0;
for (var i = 0; i < data.length; i++) {
if (distanceBetweenTwoStations(myLocationLat, myLocationLong, data[i].latitudine, data[i].longitudine,"K") <= 1 && data[i].latitudine != myLocationLat && data[i].longitudine != myLocationLong) {
Neightbours[j] = data[i];
j++;
}
}
cb(Neightbours);
});
}