Angular foreach想要等到单个电话的响应

时间:2015-06-08 20:04:27

标签: javascript angularjs

我的数组中有数千条记录。我希望对我的数组的每个元素执行一些函数。虽然我的功能很好,但我想延迟下一个项目的调用,直到第一个被正确解析。我不会有回报的相关性,无论是否真实。我只想延迟循环,直到解析单个项目。

我试过了。

 $scope.getInfo = function (funcName) {

            $http.get(Youtube.getDetails(funcName)).success(function (data, status, headers, config) { // this callback will be called asynchronously when the response is available
                if (!angular.isUndefinedOrNull(data.nextPageToken)) {
                    $scope.form.playlistToken = data.nextPageToken;
                } else {
                    $scope.form.playlistToken = "";
                    $scope.form.playlistUrl = "";
                }
                if (data.items.length > 0) {
                    angular.forEach(data.items, function (value, key) {
                        var youTubeData = Youtube.parseVideoDetail(value);
                        if (!angular.isUndefinedOrNull(youTubeData.videoId)) {
                            Video.find({filter: {where: {videoId: youTubeData.videoId}}}).$promise.then(function (data) {
                                if (data.length == 0) {
                                    Video.create(youTubeData).$promise.then(function (value, responseHeader) {
                                        $scope.items.splice(0, 0, youTubeData);
                                        youTubeData = {};
 //                                       $scope.form.url = "";
                                        toastr.success("Record saved successfully", "Success");
                                    }, function (reason) {
                                        toastr.error("Video can't be saved. Please check.", "Error");
                                    });
                                } else {
                                    duplicate.push(youTubeData.videoId);
                                    toastr.error("Video already exist with id - " + youTubeData.videoId, "Error");
                                }
                            });
                        }
                    });
                } else {
                    failed.push(funcName.values.id);
                    toastr.warning("No details found !!", "Warning");
                }
            }).error(function (data, status, headers, config) { // called asynchronously if an error occurs or server returns response with an error status.
                toastr.error("Youtube API failed to fetch data.", "Error");
            });
        }


   $scope.saveVideo = function () {
     //       var videoId = Youtube.getYoutubeParser($scope.form.url);
            var arrVid = []; // this is actually an array of 2K+ items.
            angular.forEach(arrVid, function (value, key) {
                var videoId = value.replace("?v=", "");
                Youtube.params.videoInfo.values.id = videoId;
                $scope.getInfo(Youtube.params.videoInfo);
            });
            console.log("Failed Videos" + JSON.stringify(failed));
            console.log("Duplicate Videos" + JSON.stringify(duplicate));
        }

1 个答案:

答案 0 :(得分:0)

请勿将foreach用于您的方案。你可以利用angularjs的 $ timeout或$ interval 。它们都以一定的间隔执行启动和停止操作。