我打電話從阿賈克斯成功職能工作,並試圖從函數返回值返回未在JavaScript
阿賈克斯成功是
success: function(data)
{
var distance_arr;
data = JSON.parse(data);
for (var i = 0; i < data.length; i++)
{
if(data[i].port_name != destination)
{
distance_arr = get_port_distance(data[i].port_name,source,type);
alert(distance_arr);
}
}
}
在get_port_distance功能我返回值
return distance;
通過計算get_port_distance函數中的距離。當get_port_distance我警戒距離那麼它是正確的,但是當我回到它它在阿賈克斯的成功提醒功能空白
function get_port_distance(source,destination,type)
{
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var request =
{
origin: source,
destination: destination,
travelMode: google.maps.TravelMode.DRIVING
};
var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix({
origins: [source],
destinations: [destination],
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.METRIC,
avoidHighways: false,
avoidTolls: false
}, function (response, status) {
if (status == google.maps.DistanceMatrixStatus.OK && response.rows[0].elements[0].status != "ZERO_RESULTS") {
var port_distance = response.rows[0].elements[0].distance.text;
port_distance = port_distance.replace(/\,/g,'').replace(' km',''); // To REMOVE comma(,)
return port_distance;
}
});
}
可以請你分享get_port_distance的代碼。 – Sumit
您的循環條件可能會給您空白值。 ,當你從函數 – Afsar
得到距離值時,你可能想要打破循環如果你認爲問題出現在'get_port_distance'函數中,你應該發佈它 – RizkiDPrast