多次调用同一函数时,跟踪每个$ timeout

时间:2015-09-11 18:21:40

标签: javascript angularjs timeout

$scope.fetchStatus = function (job) {
        $http
            .get('http://gtrapi/pool/checkStatus/' + sessionId + '/' + job.jobId)
            .success(function (response) {
                job[job.jobId] = response;

                if (response.status !== 'InProgress') {
                    $scope.refreshDataTimeout = $timeout($scope.fetchStatus(job), 1000);
                }
            })
            .error (function () {

            });
};

这是我的HTML代码

  <div ng-repeat="job in gtrLogs" class="each-log">
            <div class="row job-id">
                <div class="col-xs-2">
                    Job ID: {{job.jobId}}
                </div>
                <div class="col-xs-10">
                    End Point: {{job.changes.endpoint}}
                </div>
            </div>
            <div class="each-job" ng-init="fetchStatus(job)">
                <div class="job-header row">
                    <span class="col-xs-6">Job Status: <strong>{{job[job.jobId].status}}</strong>
                        <span class="glyphicon" ng-class="{'glyphicon-refresh spin' : job[job.jobId].status === 'InProgress', 'glyphicon-ok' : job[job.jobId].status === 'submitted', 'glyphicon-remove' : job[job.jobId].status === 'Aborted'}"></span>
                    </span>
                    <span class="col-xs-6">
                        <span class="glyphicon glyphicon-stop pull-right" ng-click="stopLogs()" tooltip="Stop Action"></span>
                        <span class="glyphicon glyphicon-repeat pull-right" ng-click="rollBack()" tooltip="Roll Back"></span>
                    </span>
                </div>
                <div class="logs-progress">
                    <table class="table table-striped table-condensed table-hover">
                        <thead>
                        <tr>
                            <th>
                                Message
                            </th>
                            <th>
                                Time
                            </th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr ng-repeat="row in job[job.jobId].logs">
                            <td>{{row.msg}}</td>
                            <td>{{row.time | date:'yyyy/MM/dd HH:mm:ss'}}</td>
                        </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>                      

我必须每秒更新数据并在函数中放置$ timeout。但是因为函数是从HTML中多次调用的,所以调用是嵌套的。 如何对同一份工作进行民意调查。

1 个答案:

答案 0 :(得分:0)

由于您有一个唯一的jobid,因此可以使用它来维护一组键值对,其中您的作业ID可以对应一个唯一的计数器。

var counters = [];

$scope.fetchStatus = function (job) {
    $http
        .get('http://url:9090/gtrapi/pool/checkStatus/' + sessionId + '/' + job.jobId)
        .success(function (response) {
            job[job.jobId] = response;

            if (response.status !== 'InProgress') {
                updateCounter(job.jobId);

                $scope.refreshDataTimeout = $timeout($scope.fetchStatus(job), 1000);
            }
        })
        .error (function () {
        });
};

function updateCounter(jobId) {
    var exists = false,
        jobId = parseInt(jobId);

    for (var i in counters) {
        if (counters[i].id === jobId) {
            projects[i].counter++;
            exists = true;
            break;
        } 
    }  

    if (!exists) {
        counters.push({id: jobId, counter: 0});
    }
}