简体   繁体   English

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

[英]keep track of each $timeout when calling the same function multiple times

$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 () {

            });
};

Here is my HTML code 这是我的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>                      

I have to update the data every second and placed $timeout in the function. 我必须每秒更新一次数据,并将$ timeout放在函数中。 But because the function is being called multiple times from the HTML the calls are nested. 但是,由于从HTML多次调用了该函数,因此嵌套了这些调用。 How Do I keep polling with respect to the same job. 如何针对同一工作进行轮询。

Since you have a unique jobid, use can use that to maintain an array of key value pairs where your job id can correspond to a unique counter. 由于您具有唯一的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});
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM