Javascript 在*JSON完成后返回数组*?

Javascript 在*JSON完成后返回数组*?,javascript,jquery,ajax,Javascript,Jquery,Ajax,在推送完所有标记后,如何使此函数返回整个数组 function XTW_getLocations(data, div_id, map) { var markers = []; var marker; var latLngBounds = new google.maps.LatLngBounds(); $(div_id).empty(); $.getJSON('GetLocations', "country=" + data.id, func

在推送完所有标记后,如何使此函数返回整个数组

function XTW_getLocations(data, div_id, map) {
    var markers = [];
    var marker;
    var latLngBounds = new google.maps.LatLngBounds();
    $(div_id).empty();
    $.getJSON('GetLocations', "country=" + data.id,
        function(data){
            $.each(data, function(index, data) {
                latLngBounds.extend(new google.maps.LatLng(data.location.latitude, data.location.longitude));
                $(div_id).append( new Option(data.name, data.id ) );
                marker = createMarker(data, icon, html, map);
                markers.push(marker);
            });
            map.fitBounds(latLngBounds);
        });
    return markers;
}

您不能返回它,因为它是异步的(当响应返回时,它会在函数已经返回之后填充)

但是,您可以将其用于其他用途,例如:在准备/填充时将其传递给另一个函数,如下所示:

function XTW_getLocations(data, div_id, map) {
    var markers = [];
    var marker;
    var latLngBounds = new google.maps.LatLngBounds();
    $(div_id).empty();
    $.getJSON('GetLocations', "country=" + data.id,
        function(data){
            $.each(data, function(index, data) {
                latLngBounds.extend(new google.maps.LatLng(data.location.latitude, data.location.longitude));
                $(div_id).append( new Option(data.name, data.id ) );
                marker = createMarker(data, icon, html, map);
                markers.push(marker);
            });
            anotherFunction(markers);
            map.fitBounds(latLngBounds);
        });
}
如果没有,AJAX查询(
getJSON
)是异步的,这意味着一旦发出调用,就会超出正常的处理顺序,而是使用回调,就像调用
getJSON
时一样:

function XTW_getLocations(data, div_id, map, callBack) {
    var markers = [];
    var marker;
    var latLngBounds = new google.maps.LatLngBounds();
    $(div_id).empty();
    $.getJSON('GetLocations', "country=" + data.id,
        function(data){
            $.each(data, function(index, data) {
                latLngBounds.extend(new google.maps.LatLng(data.location.latitude, data.location.longitude));
                $(div_id).append( new Option(data.name, data.id ) );
                marker = createMarker(data, icon, html, map);
                markers.push(marker);
            });
            map.fitBounds(latLngBounds);
            callBack(markers); //Callback goes here
        });
    //return markers;
}
现在,在调用
XTW\u getLocations
时,您需要在调用中添加回调:

XTW_getLocations({some:'data'},'#map','map.png',function(markers){
    //Handle markers here
})

因此,将它放在getJSON()内部,在每个()之后,直到每个()都完成后,才会调用另一个函数()。但是,如何将它返回到一开始调用XTW_getLocations的变量?@M.E-第一条注释是正确的,第二条注释是:如果没有,则将数据传递到其他地方,只要数据可用,就可以将数据传递到任何需要的地方。您可以将回调函数传递到
XTW\u getLocations
中,也可以始终将其传递到内部的同一个函数中,但不返回。要做到这一点,唯一的方法是使其同步,从而锁定浏览器。。。。这是一个问题,因为阵列用于存储。我需要在用户请求时提供它,以便进行新的搜索。@M.E-在数据返回之前,您无法获得它,这需要时间,我不知道如何解释更好的方法…AJAX不是即时的。不幸的是,没有。。。您将永远无法将标记返回到特定的任何位置,但是您可以在回调中处理数据,或者如果您想重用它,可以使用命名函数来代替匿名函数。我很快尝试了它,并返回XTW_getLocations(x,x,x,函数(标记){return markers;})似乎有效。但事实并非如此?这只是运气?事实上我又试了一次,但是JSON返回了2557个对象(也就是说,让我的笔记本电脑呕吐),回调的数量相等,所以看起来效果很好。