I need to run 20-200 ajax requests in a loop, but not damage google.maps.Geocoder I want to set a delay of 10 seconds between each call. However, the ajax request is asynchronous, so I call the next ajax request in response received after the previous one. If I get an answer too quickly, a delay must occur.
Here is the code that I have written so far:
... $scope.addressList = ....; $scope.taskCount = $scope.addressList.length; geoTaskLoopAsync(); function geoTaskLoopAsync(){ // on success douncount taskCount var geo = new google.maps.Geocoder(); geocoder.geocode( { 'address': address }, function(results, status) { $scope.$apply( function () { // do something with response if($scope.taskCurr <= $scope.taskCount){ $scope.taskCurr++; return geoTaskLoopAsync(); } return; }); });
So what's next?
You can add something like:
stop = $timeout(function() { if($scope.taskCurr <= $scope.taskCount){ geoTaskLoopAsync(); } else { $timeout.cancel(stop); } }, 10000);
or do i have another way?
Thanks,
source share