在角度js中的特定间隔(5秒)后添加自动刷新

时间:2017-08-20 16:25:59

标签: angularjs

我有一个页面,其中有多个标签。我想添加一个功能,即在固定的持续时间后自动重新加载选项卡。我有以下内容:

<uib-tab index="$index + 1" ng-repeat="environmentRegionTab in ctrl.environmentRegionTabs"
         heading="{{environmentRegionTab.environmentRegionName}}"
         select="ctrl.onEnvironmentRegionSelect(environmentRegionTab.id);">
    <div class="panel-body tab-content">
        <div class="alert alert-success" ng-show="ctrl.deployStatus[ctrl.environmentRegion.name].show">
            <strong>Launched deployment with execution id
                {{ctrl.deployStatus[ctrl.environmentRegion.name].id}}</strong>
        </div>

...................

以下控制器:

export function ServiceDetailController(ecsServiceResponse, teamListResponse, productListResponse, businessSubOwnerListResponse, serviceInstancesResponse, businessOwnerListResponse, EcsService, SecretsService, $location, $stateParams, $uibModal, $scope, $state, $window) {
    'ngInject';
    var self = this;
    var serviceInstanceId;
self.ecsAuthControl = {};
self.initialize = _initialize;
self.clearMessages = _clearMessages();
self.onEnvironmentRegionSelect = _onEnvironmentRegionSelect;

$scope.reloadRoute = function() {
    $state.reload();
};

function _onEnvironmentRegionSelect(serviceInstanceId) {
    self.selectedserviceInstanceId = serviceInstanceId;
    if (serviceInstanceId) {
        $location.search('serviceInstanceId', serviceInstanceId);
        _loadEnvironmentRegion();
    } else {
        $location.search('serviceInstanceId', null);
        _loadSummary();
    }
}

}

我无法理解如何添加固定的持续时间?我还想显示一个从5到0的计数器,然后重新加载页面。我该怎么做?我声明了重载功能,但我无法弄清楚如何添加固定计时器?谢谢!

2 个答案:

答案 0 :(得分:2)

在angularjs中使用$interval服务:

$interval(function () {
   $scope.reloadRoute();
}, 5000);

(确保将$ interval作为依赖项传递给控制器​​)​​

Example Plunker

答案 1 :(得分:1)

这是您实现此功能的最安全方式之一。

刷新的功能: -

var poll = function() {
    console.log("polling");
    $scope.doRefresh(); // Your refresh logic
};

StartPollar

调用民意调查
var startPoller = function() {
            if (angular.isDefined(stop)) {
            stopPoller();
           }
    stop = $interval(poll, $scope.intervalTime); //$scope.intervalTime : refresh interval time
 };

如果你想停止

var stopPoller = function() {
        if (angular.isDefined(stop)) {
            $interval.cancel(stop);
            stop = undefined;
            console.log("cancelled poller operation");
        } else {
            console.log("do nothing");
        }
};