Help Needed with AJAX Request Timeout Issue
I recently created an array and tried to send an ajax request using setTimeout. However, I encountered a problem where I couldn't get the parameter in setTimeout. The console log showed that the variable 'i' was undefined. Can anyone guide me on how to resolve this issue?
Console Log Result:
i undefined
Javascript Code:
$scope.type = ["st", "ct", "sc", "rm", "bg", "sf", "fl", "sq", "pr", "cr", "vl", "fd"];
for (var i = 0; i < $scope.type.length; i++) {
setTimeout(function (i) {
console.log("i", i);
$scope.params = {
lat: $scope.lat,
lng: $scope.lng,
time: $scope.time,
type: $scope.type[i]
}
console.log("params", $scope.params);
return;
$.ajax({
type: 'post',
url: "bz.php",
dataType: 'json',
async: true,
cache: false,
data: $scope.params,
success: function (data) {
if (data.response) {
console.log("data.response", data.response);
return;
if (!$scope.$$phase) $scope.$apply();
} else if (data.response == null) {
} else if (data.error) {
}
},
error: function (data) {
}
});
}.bind(this), i * 2000);
}