【问题标题】:keep track of each $timeout when calling the same function multiple times多次调用同一个函数时跟踪每个 $timeout
【发布时间】:2015-09-11 18:21:40
【问题描述】:
$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 中多次调用该函数,所以调用是嵌套的。 我如何继续针对同一工作进行投票。

【问题讨论】:

    标签: javascript angularjs timeout


    【解决方案1】:

    由于您有一个唯一的 jobid,因此可以使用它来维护一个键值对数组,其中您的 job 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});
        }
    }
    

    【讨论】:

    • 确切的问题是我的 $timeout 是嵌套的(调用无限次),因为 $scope.fetchStatus() 在视图中被多次调用,因为函数在 ng-repeat 中。因此,在每个 jobID 的帮助下,我想针对该特定 jobID 调用 $scope.fetchStatus() 函数,并每隔一秒就 jobID 进行轮询。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-21
    • 1970-01-01
    • 2011-05-09
    • 1970-01-01
    相关资源
    最近更新 更多