我在我的谷歌地圖代碼中處理這個問題,但它實際上是一個架構問題。在某些時候,由於谷歌地圖有這麼多的請求限制了響應,並且在那一點上我想要延遲另一個請求,但是當我再次調用function2時,它說 表示「數組未定義」。javascript變量在函數內部丟失
function1() {
var array = JSON.parse(xmlhttp.responseText);
for (i; i < length; < i++) {
function2(array[i].one, array[i].two);
}
function3() {
//render directions
}
function2(start, end) {
directionsService.route({
origin: start,
destination: end,
},
function (result, status) {
if (status == google.maps.DirectionsStatus.OK)
function3(result);
else if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT)
var functionStr = "function2(array[i].one" + ',' + "array[i].two)";
setTimeout(functionStr, 5000);
});
}
}