注销时停止时间间隔服务

时间:2017-03-16 12:26:23

标签: javascript angularjs

我正在使用角度js。我已经创建了一个控制器函数并且相反地调用它,因此设置了timeinterval。而且我必须在全球范围内运行如此创建的服务。例如:

以下是 NotificationController

app.controller('NotificationController', function($scope, $http, notificationService, $interval) {
    $interval(function() {
        notificationService.userNotification().then(function(response){
           $scope.notification = response.data.notification;
           $scope.total_notifcation = $scope.notification.length;
        });
    },5000);

    this.update_notification = function(notification_id,index) {
        var is_read_data = $.param({notification_id:notification_id});

        $http({
            method: 'POST',
            url: apiUrl + "tickets/update_notification",
            data: is_read_data,
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            }
        }).then(function(response) {
            $scope.notification.splice(index,1);
            $scope.total_notifcation = $scope.notification.length;
        });
    }
});

以下是 notificationService

app.service("notificationService",function($http,$q,$window) {
    this.userNotification = function() {
        return $http({
            method:"GET",
            url:apiUrl + "tickets/notification"
        });
    }
})

现在在 authController 中有一个注销函数,如下所示:

vm.logout = function() {
    if($window.sessionStorage.getItem('userInfo'))
    {
        $window.sessionStorage.clear();
        $state.go('login');
    }
}

此处,注销此服务后会连续调用。当我刷新页面而不是停止。但是当我打电话给注销时,我想阻止它。那么我该怎么做才能阻止它呢?

2 个答案:

答案 0 :(得分:1)

在NotificationController中,保存间隔返回的保证。

var interval = $interval(function() {
    notificationService.userNotification().then(function(response){
       $scope.notification = response.data.notification;
       $scope.total_notifcation = $scope.notification.length;
    });
},5000);

当控制器被销毁时,取消间隔。

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

如果您的控制器永远不会被卸载,即使在注销时,您也可以在$ rootScope中保存间隔引用,如下所示:

$rootScope.notificationInterval = $interval(function() {
notificationService.userNotification().then(function(response){
   $scope.notification = response.data.notification;
   $scope.total_notifcation = $scope.notification.length;
  });
},5000);

并在注销时停止间隔

vm.logout = function() {
  if($window.sessionStorage.getItem('userInfo'))
  {
      $window.sessionStorage.clear();
      $rootScope.notificationInterval.cancel();
      $state.go('login');
  }
} 

答案 1 :(得分:1)

$ interval返回一个承诺。将此承诺分配给变量并将其通过 NotificationController 中的任何共享服务传递给 authController

var promise = $interval(function() {
    notificationService.userNotification().then(function(response){
       $scope.notification = response.data.notification;
       $scope.total_notifcation = $scope.notification.length;
    });
},5000);

sharedService.currentIntervalPromise = promise;

现在从 authController 获取共享服务的承诺,并在注销功能中取消它。

vm.logout = function() {
  var promise = sharedService.currentIntervalPromise;
  if($window.sessionStorage.getItem('userInfo'))
  {
      $interval.cancel(promise);
      $window.sessionStorage.clear();
      $state.go('login');
  }
}