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

$qこれは、promise とサービスの優れた使用例のようです。

これは、Promise の使用がどのように見えるかの大まかなスケッチです。10 秒の遅延を処理する遅延サービスと、ジオコーディングを処理するマップ サービスを宣言します。どちらのサービスも promise を返し、コントローラーは を使用して promise を組み合わせて、$q.all()Google API 呼び出しの間に少なくとも 10 秒の遅延があることを確認できます。

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 に答える