programing

시간을 클리어 또는 정지하는 방법angularjs 간격?

instargram 2023. 4. 2. 09:44
반응형

시간을 클리어 또는 정지하는 방법angularjs 간격?

데모를 만들고 있습니다.서버에서 데이터를 가져오고 있습니다.$interval이제 이 작업을 중지/취소해야 합니다.

어떻게 하면 좋을까요?프로세스를 다시 시작해야 할 경우 어떻게 해야 합니까?

둘째, 한 가지 질문이 더 있습니다.일정 시간 후 서버에서 데이터를 가져오고 있습니다.사용할 필요가 있습니까?$scope.apply또는$scope.watch?

여기 내 플런커가 있다.

  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

반환된 약속을 간격별로 저장하고 사용할 수 있습니다.$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>

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

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

Interval Store Promise to variable을 작성하는 경우:

var p = $interval(function() { ... },1000);

또한 간격을 중지/삭제하려면 다음을 사용하십시오.

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

언급URL : https://stackoverflow.com/questions/26447923/how-to-clear-or-stop-timeinterval-in-angularjs

반응형