我想在for循环中访问回调函数使用的i的值。Javascript - 如何使用迭代器在for循环中使用回调
我该怎么做?
for (var i = 0; i < a.length; i++)
{
calcRoute(fixedLocation, my_cities[i].address, function(response) {
// i want here to have the current "i" here
});
}
这就要求...
function calcRoute(x, y, callback) {
var start = x;
var end = y;
var request = {
origin:start,
destination:end,
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.METRIC,
optimizeWaypoints: true
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
callback(response);
} else {
alert("City unknown.")
}
});
}