Estoy intentando en el ciclo for acceder al valor de la i con la que utiliza la función de devolución de llamada.Javascript - cómo trabajar con el iterador en un bucle for con devoluciones de llamada
¿Cómo puedo hacer esto?
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
});
}
que se pide ...
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.")
}
});
}