AngularJS:为什么不让我$interval stop ?

AngularJS: Why won't me $interval stop?

本文关键字:interval stop 为什么不 AngularJS      更新时间:2023-09-26

我已经看了文档,但现在是凌晨3点,我的课快结束了。这是我的控制器:

controller('makeDashCtrl', function ($scope, $rootScope, $cookies, $location, $http, $interval) {
    var userId  = $cookies.get('user_id');
    var orgId   = $cookies.get('org_id');
    $http.get('/api/consolidateProfile/').then(function(data){
      console.log(data);
    }, function(res){
      console.log(res + " - Eh, consolidateProfile probably timed out, no biggie")
    })

    var testStart = $interval(function(){
      $http.get('/api/testProgress/' + userId).then(function(obj){
        $scope.message = obj.data.message;
        $scope.sub = obj.data.sub;
        if(obj.data.nextPhase){
          console.log("Should be cancelling...");
          nextScrape();
          $interval.cancel(testStart);  // This one cancels fine
        }
      }, function(err){
        $interval.cancel(testStart);  
      });  
    }, 1000);
    function nextScrape(){
       console.log("In checkScraperadsfsdfads!")
      var checkScraper = $interval(function(){
        console.log("In checkScraper REAL!")
        $http.get('/api/checkScraper/' + userId).then(function(obj){
          var msg = JSON.parse(obj.data);
          console.log("Got some data!")
          console.log(obj);
          if(msg.doneFbs){
            $scope.fbMsg = "We've finished gathering " + msg.doneFbs + " Facebook accounts";
          }
          if(msg.doneTwits){
            $scope.twitMsg = "We've finished gathering " + msg.doneTwits + " Twitter accounts";
          }
          $scope.message = msg.message;
          $scope.sub = msg.sub;

          if(msg.finished){
            $interval.cancel(checkScraper); // This does NOT cancel
            console.log("Scraping Done!")
            $location.path('/dashboard') // This successfully redirects to the next page
          }

        },function(err){
          console.log("There was an error in Checkscraper ")
          console.log(err)
          $interval.cancel(checkScraper);  // This does NOT cancel when there's an error
        });  
      }, 3000)
    }
  })

请参阅上面代码中的注释。也许这是nextScrape()函数的问题,但我不能让$interval取消。即使当$location.path()切换到下一页时,间隔仍在运行。

我在这里做错了什么?

如果调用的返回时间超过1秒,则可能同时有多个ajax请求。不使用$interval,为什么不进行一次调用,并在第一次调用完成时使用$tiemout来安排第二个调用?

function checkProgress() {
  $http.get('/api/testProgress/' + userId).then(function(obj){
    $scope.message = obj.data.message;
    $scope.sub = obj.data.sub;
    if(obj.data.nextPhase){
      // move on to next phase
      nextScrape();
    } else {
      // not done, schedule another check
      $timeout(checkProgress, 1000);
    }
  }, function(err){
    // error, you cancel, but maybe schedule another progress check?
  });  
}

我怀疑代码在收到Ajax的响应之前进行了多次调用。这意味着ajax的响应时间超过一秒钟。发生这种情况是因为您提到了从服务器再次池化数据的时间非常少。

要在页面重定向后停用$interval,可以在scope上使用$destroy事件。事件需要手动清除,因为除非我们分离它们,否则它们不会消失。您可以在离开页面时停止interval。销毁作用域event

上的事件

$scope.$on('$destroy', function(){
   $interval.cancel(testStart);
})