本文介绍了$间隔功能继续当我改变路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个关于问题的 $间隔当我改变路线功能继续。

I am getting a problem regarding an $interval function continues when I change routes.

这是code:

  x = 2;

 var multiply = function () {
     x = x * 2;
     if (x == 134217728) {
         $interval.cancel($scope.stop);
         $scope.stop = 0;
     }
 }


 $scope.Result = function () {
     $scope.stop = $interval(multiply, 1000);
 }

 $scope.Result();

在 X< 134217728 ,我改变路线移动到另一个页面。问题是, $间隔不路由变化后停止。我的承诺存储在变量 $范围模型停止。我觉得 $范围之后的路线改变,这就是为什么 $间隔继续不破坏。

When x<134217728, I change the route to move to another page. The problem is that $interval doesn't stop after the route changes. I store the promise in the variable stop in the $scope model. I think $scope doesn't destroy after routes change and that is why $interval continues.

我怎样才能解决这个问题呢?

How can I solve this issue ?

推荐答案

您可以使用 $ interval.cancel($ scope.stop); $ locationChangeSuccess 调用在您的控制器/服务。

you can use $interval.cancel($scope.stop); when $locationChangeSuccess is invoked in your controller/service.

例如

controller('MyController', function($rootScope, $interval) {
  $scope.stop = $interval(yourOperation, 1000);

  var dereg = $rootScope.$on('$locationChangeSuccess', function() {
    $interval.cancel($scope.stop);
    dereg();
  });

  function yourOperation() {}
};

这篇关于$间隔功能继续当我改变路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 22:12