Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Angular foreach希望等待单个调用的响应_Javascript_Angularjs - Fatal编程技术网

Javascript Angular foreach希望等待单个调用的响应

Javascript Angular foreach希望等待单个调用的响应,javascript,angularjs,Javascript,Angularjs,我的数组中有数千条记录。我希望对数组的每个元素执行一些函数。虽然我的函数工作得很好,但我想延迟下一项的调用,直到第一项被正确解析。我也没有回报的相关性,不管是真是假。我只想延迟循环,直到单个项被解析 我试过了 $scope.getInfo = function (funcName) { $http.get(Youtube.getDetails(funcName)).success(function (data, status, headers, config) { /

我的数组中有数千条记录。我希望对数组的每个元素执行一些函数。虽然我的函数工作得很好,但我想延迟下一项的调用,直到第一项被正确解析。我也没有回报的相关性,不管是真是假。我只想延迟循环,直到单个项被解析

我试过了

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

不要在您的场景中使用foreach。您可以利用angularjs的$timeout或$interval。它们都以一定的间隔执行启动和停止操作。

但我希望在当前迭代完成后执行下一次迭代。