javascript 在angularjs导航期间停止时间间隔

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20392346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 18:23:39  来源:igfitidea点击:

Stop time interval during navigation in angularjs

javascriptangularjssetinterval

提问by Ramesh Rajendran

Currently i am using angular js

目前我正在使用 angular js

I have one get method to get data from server side in default.htmlpage

我有一种获取方法可以从default.html页面中的服务器端获取数据

function bindActiveLoans() {
            $http.get(rootUrlSingleEscaped + '/cooperativa/allunfundedloans/?authId=' + userInfo.UserId)
                 .success(function (response) {
                     if (response.Loans.length == 0) {
                         $scope.IsValues = false;
                         $scope.$apply();
                         return true;
                     }
                     $scope.IsValues = true;
                     $scope.unfundedLoans = response;
                 });
        };

        setInterval(function () {
            $scope.$apply(bindActiveLoans());
        }, 5000);

The above function helps to get the data from server side method in every 5 seconds.

上述函数有助于每 5 秒从服务器端方法获取数据。

Here, I have an issue.

在这里,我有一个问题。

Additionally I have some more pages, like

另外我还有一些页面,比如

  • default2.html,default3.html,default4.html,default5.html.

    The timer is still running while i navigation default.htmlpage to default1.htmlpage. I want to only that timer running in default.htmlpage.If i go to another page,then the timer should stop. How can we achieve it?

  • default2.html,default3.html,default4.html,default5.html。

    当我导航default.html页面到default1.html页面时,计时器仍在运行。我只想在default.html页面中运行那个计时器。如果我转到另一个页面,那么计时器应该停止。我们怎样才能实现它?

回答by nacholibre

From the angularjs example in the $interval API page.

来自$interval API 页面中的 angularjs 示例。

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

    $interval.cancel(interval);

With $interval.cancel(var) you can stop the interval and if you want to stop it when navigating to another page you should try something like this.

使用 $interval.cancel(var) 你可以停止间隔,如果你想在导航到另一个页面时停止它,你应该尝试这样的事情。

var interval = null;

module.controller('SomeController', '$interval', function($interval) {
    interval = $interval(function() {
        ...
    }, 2000);
}).run(['$rootScope', '$interval', function($rootScope, $interval) {
    $rootScope.$on('$routeChangeStart', function() {
        $interval.cancel(interval);
    });
}]);

回答by Jayantha Lal Sirisena

You can listen to the $routeChangeStartevent and do the clearing of the interval depending on the route parameters,

您可以监听$routeChangeStart事件并根据路由参数清除间隔,

  $scope.$on('$routeChangeStart', function (scope, next, current) {
           if(<next is not default.aspx>){
           // clear interval here
            }
  });

回答by simply-put

call clearInterval function when navigating from default page

从默认页面导航时调用 clearInterval 函数

e.g.

例如

var myVar = setInterval(function () {
        $scope.$apply(bindActiveLoans());
    }, 5000);


function myStopFunction()
{
clearInterval(myVar);
} 

Call myStopFunction

调用 myStopFunction