如何在angularjs中清除或停止muliple timeInterval?

时间:2017-06-09 10:26:47

标签: javascript angularjs angularjs-scope

我有行列表说多个按钮名称在视图中重试,点击重试按钮后调用angular $ interval,我在$ interval内调用了一个API,当retryCount为4时我想停止$ interval ,这不是单个$ interval调用,它的多个$ interval调用,我的代码只能单击Retry按钮,如果我点击多个Retry按钮,$ interval stopped not working

self.retryJob = function (jobId,row) {
            row.submitButtonText = "Retrying..";  
            JobService.submitJob(jobId).then(function (res) {
                if(!res.data.error){
                    row.status = "retrying";
                    row.retryCount = 0;
                    row.submitButtonText = "Retry"
                    //$scope.stop();
                    promise = $interval(function() {
                                $scope.refreshItems(jobId,row);
                              },1000);
                }else{
                    row.submitButtonText = "Error:Try Again";
                }
            }).catch(function (err) {

            });
        };
        $scope.refreshItems = function(jobId,row){
            JobService.getJobById(jobId).then(function (res) {
                console.log("res is ",res.data);
                if(!res.data.error){
                    row.status = res.data.status;
                    row.retryCount = res.data.retryCount;
                    //console.log("res.data.status="+jobId,res.data.status);
                    if(res.data.retryCount=="4" && (res.data.jobId==jobId)){
                        console.log("stoped api call");
                        //// Not working need to modify later
                        //$scope.stop();
                        $interval.cancel(promise);
                    }
                }else{
                }
            }).catch(function (err) {
                //row.killButtonText = "Error:Try Again";
            })
        }

2 个答案:

答案 0 :(得分:0)

试试这个:

self.retryJob = function (jobId,row) {
        row.submitButtonText = "Retrying..";  
        JobService.submitJob(jobId).then(function (res) {
            if(!res.data.error){
                row.status = "retrying";
                row.retryCount = 0;
                row.submitButtonText = "Retry"
                //$scope.stop();
                $rootScope.promise = $interval(function() {
                            $scope.refreshItems(jobId,row);
                          },1000);
            }else{
                row.submitButtonText = "Error:Try Again";
            }
        }).catch(function (err) {

        });
    };
    $scope.refreshItems = function(jobId,row){
        JobService.getJobById(jobId).then(function (res) {
            console.log("res is ",res.data);
            if(!res.data.error){
                row.status = res.data.status;
                row.retryCount = res.data.retryCount;
                //console.log("res.data.status="+jobId,res.data.status);
                if(res.data.retryCount=="4" && (res.data.jobId==jobId)){
                    console.log("stoped api call");
                    //// Not working need to modify later
                    //$scope.stop();
                    $interval.cancel($rootScope.promise);
                }
            }else{
            }
        }).catch(function (err) {
            //row.killButtonText = "Error:Try Again";
        })
    }

答案 1 :(得分:0)

确切的答案可能取决于您的JobService正在做什么&返回,但仍然可以做到这一点:

    self.retryJob = function (jobId,row) {
        row.submitButtonText = "Retrying..";  
        JobService.submitJob(jobId).then(function (res) {
            if(!res.data.error){
                row.status = "retrying";
                row.retryCount = 0;
                row.submitButtonText = "Retry"
                //$scope.stop();
                var o = {};
                o.interval = $interval(function() {
                            $scope.refreshItems(jobId,row,o);
                          },1000);
            }else{
                row.submitButtonText = "Error:Try Again";
            }
        }).catch(function (err) {

        });
    };
    $scope.refreshItems = function(jobId,row,o){
        JobService.getJobById(jobId).then(function (res) {
            console.log("res is ",res.data);
            if(!res.data.error){
                row.status = res.data.status;
                row.retryCount = res.data.retryCount;
                //console.log("res.data.status="+jobId,res.data.status);
                if(res.data.retryCount=="4" && (res.data.jobId==jobId)){
                    console.log("stoped api call");
                    //// Not working need to modify later
                    //$scope.stop();
                    $interval.cancel(o.interval);
                }
            }else{
            }
        }).catch(function (err) {
            //row.killButtonText = "Error:Try Again";
        })
    }