4

我需要循环运行 20-200 个 ajax 请求,但为了不伤害google.maps.Geocoder我想在每次调用之间设置延迟 10 秒。但是ajax请求是异步的,因此我ajax在前一个响应成功时调用下一个请求。如果我的反应太快,应该会延迟。

这是我到目前为止编写的代码:

 ...
 $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;
            });
        });

那么接下来呢?

我可以添加类似的东西:

 stop = $timeout(function() { 
         if($scope.taskCurr <= $scope.taskCount){               
            geoTaskLoopAsync();
        } else {
            $timeout.cancel(stop);
        }                
    }, 10000);

还是我有其他方式?

谢谢,

4

1 回答 1

7

This seems like a great use case for promises and the $q service.

Here's a rough sketch of what using promises might look like. I declare a delay service to handle the 10 second delay and a maps service to handle the geocoding. Both services return promises, and the controller can combine the promises using $q.all() to ensure that there is at least a 10 second delay between Google API calls.

angular.module( /* load your module */ ).service('delay', ['$q', '$timeout', function ($q, $timeout) {
    return {
        start: function () {
            var deferred = $q.defer();
            $timeout(deferred.resolve, 10000);
            return deferred.promise;
        }
    };
}]);

angular.module( /* load your module */ ).service('maps', ['$q', function ($q) {
    var geocoder = new google.maps.Geocoder();
    return {
        geocode: function (address) {
            var deferred = $q.defer();

            geocoder.geocode({
                'address': address
            }, function (results, status) {
                deferred.resolve(results);
                // Should also reject if AJAX errors.
            });

            return deferred.promise;
        }
    };
}]);


angular.module( /* load your module */ ).controller('geocodingController', ['delay', 'maps', '$q', function (delay, maps, $q) {
    var addresses = [/* An array of addresses to geocode */],
        addressIndex = 0,
        geocodeAddresses = function geocodeAddresses() {
            // Use $q.all so that the callback will execute when the geocoding is done AND 10 seconds have passed.
            $q.all([delay.start(), maps.geocode(addresses[addressIndex])]).then(function (results) {
                addressIndex += 1;
                var geocodedData = results[1]; // The geocode result is in the second index.

                // Do something with the results.

                if (addressIndex < addresses.length) {
                    geocodeAddresses();
                }
            });
        };

    // Kick off the AJAX requests.
    geocodeAddresses();
}]);
于 2013-05-09T14:45:21.993 回答