2014-10-19 30 views
81

我正在做一个演示,我正在使用$interval定期的时间间隔从服务器获取数据。现在我需要停止/取消这个。如何清除或停止angularjs中的timeInterval?

我该如何做到这一点?如果我需要重新启动流程,我应该怎么做?其次,我还有一个问题:我在时间间隔后从服务器获取数据。有没有必要使用$scope.apply$scope.watch

这里是我的plunker:

app.controller('departureContrl',function($scope,test, $interval){ 
    setData(); 

    $interval(setData, 1000*30); 

    function setData(){ 
     $scope.loading=true; 
    test.stationDashBoard(function(data){ 
     console.log(data); 
     $scope.data=data.data; 
     $scope.loading=false; 
     //alert(data); 
    },function(error){ 
     alert('error') 
    }) ; 

    } 
}); 

http://plnkr.co/edit/ly43m5?p=preview

回答

129

可以存储由间隔返回的承诺,用$interval.cancel()这一承诺,这将取消这一承诺的时间间隔。要委派时间间隔的开始和停止,您可以创建start()stop()函数,只要您想停止并从特定事件再次启动它们即可。我在下面创建了一个片段,通过在事件(例如ng-click)和控制器中使用事件的视图实现它来显示启动和停止时间间隔的基础知识。

angular.module('app', []) 
 

 
    .controller('ItemController', function($scope, $interval) { 
 
    
 
    // store the interval promise in this variable 
 
    var promise; 
 
    
 
    // simulated items array 
 
    $scope.items = []; 
 
    
 
    // starts the interval 
 
    $scope.start = function() { 
 
     // stops any running interval to avoid two intervals running at the same time 
 
     $scope.stop(); 
 
     
 
     // store the interval promise 
 
     promise = $interval(setRandomizedCollection, 1000); 
 
    }; 
 
    
 
    // stops the interval 
 
    $scope.stop = function() { 
 
     $interval.cancel(promise); 
 
    }; 
 
    
 
    // starting the interval by default 
 
    $scope.start(); 
 
    
 
    // stops the interval when the scope is destroyed, 
 
    // this usually happens when a route is changed and 
 
    // the ItemsController $scope gets destroyed. The 
 
    // destruction of the ItemsController scope does not 
 
    // guarantee the stopping of any intervals, you must 
 
    // be responsible for stopping it when the scope is 
 
    // is destroyed. 
 
    $scope.$on('$destroy', function() { 
 
     $scope.stop(); 
 
    }); 
 
      
 
    function setRandomizedCollection() { 
 
     // items to randomize 1 - 11 
 
     var randomItems = parseInt(Math.random() * 10 + 1); 
 
     
 
     // empties the items array 
 
     $scope.items.length = 0; 
 
     
 
     // loop through random N times 
 
     while(randomItems--) { 
 
     
 
     // push random number from 1 - 10000 to $scope.items 
 
     $scope.items.push(parseInt(Math.random() * 10000 + 1)); 
 
     } 
 
    } 
 
    
 
    });
<div ng-app="app" ng-controller="ItemController"> 
 
    
 
    <!-- Event trigger to start the interval --> 
 
    <button type="button" ng-click="start()">Start Interval</button> 
 
    
 
    <!-- Event trigger to stop the interval --> 
 
    <button type="button" ng-click="stop()">Stop Interval</button> 
 
    
 
    <!-- display all the random items --> 
 
    <ul> 
 
    <li ng-repeat="item in items track by $index" ng-bind="item"></li> 
 
    </ul> 
 
    <!-- end of display --> 
 
</div> 
 

 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

+0

@thanks答复..是有什么需要补充申请,或者当我从服务reqular的时间间隔获取时间 – user944513 2014-10-19 06:54:38

+0

不,你看数据如果您使用'$ interval'服务,则不需要使用'apply()',因为它可以为您提供服务。但是如果你使用内建的'setInterval()',那么你可能不得不使用'$ scope。$ apply()'。 – ryeballar 2014-10-19 07:00:24

+0

非常好的解决方案。 ''destroy'和'http-auth-interceptor'一起工作良好,就像我想在auth失败期间停止时间间隔(如过期会话)并在成功验证后再次启动一样。感谢您提供这个出色的解决方案。 – Neel 2015-11-14 15:26:38

31
var interval = $interval(function() { 
    console.log('say hello'); 
}, 1000); 

$interval.cancel(interval); 
+0

简单明了的解决方案,完美的工作 – 2017-05-27 21:14:17

8
var promise = $interval(function(){ 
    if($location.path() == '/landing'){ 
     $rootScope.$emit('testData',"test"); 
     $interval.cancel(promise); 
    } 
},2000); 
+0

因为URL可能会在将来发生变化,所以在使用'$ location.path()'之前我会考虑三次,而不是状态名 – 2016-06-15 08:17:14

1
$scope.toggleRightDelayed = function(){ 
     var myInterval = $interval(function(){ 
      $scope.toggleRight(); 
     },1000,1) 
     .then(function(){ 
      $interval.cancel(myInterval); 
     }); 
    };