首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何清除或停止angularjs中的timeInterval?

如何清除或停止angularjs中的timeInterval?
EN

Stack Overflow用户
提问于 2014-10-19 14:24:32
回答 5查看 110K关注 0票数 93

我正在制作一个演示,在此演示中,我使用$interval定期从服务器获取数据,现在我需要停止/取消此操作。

我如何才能做到这一点?如果我需要重新启动进程,我应该怎么做?

其次,我还有一个问题:我在规定的时间间隔后从服务器获取数据。是否需要使用$scope.apply$scope.watch

这是我的短裤:

代码语言:javascript
复制
  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

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2014-10-19 14:51:03

您可以存储间隔返回的promise,并对该promise使用$interval.cancel(),这将取消该promise的间隔。要委托间隔的开始和停止,您可以在想要从特定事件停止和重新启动时创建start()stop()函数。我已经在下面创建了一个代码片段,通过使用事件(例如ng-click)在视图中和控制器中实现,显示了开始和停止间隔的基本知识。

代码语言:javascript
复制
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)); 
      }
    }
  
  });
代码语言:javascript
复制
<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>

票数 160
EN

Stack Overflow用户

发布于 2015-06-25 21:56:36

代码语言:javascript
复制
var interval = $interval(function() {
  console.log('say hello');
}, 1000);

$interval.cancel(interval);
票数 39
EN

Stack Overflow用户

发布于 2016-02-26 16:18:11

代码语言:javascript
复制
var promise = $interval(function(){
    if($location.path() == '/landing'){
        $rootScope.$emit('testData',"test");
        $interval.cancel(promise);
    }
},2000);
票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26447923

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档