Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在angularjs中导航期间的停止时间间隔_Javascript_Angularjs_Setinterval - Fatal编程技术网

Javascript 在angularjs中导航期间的停止时间间隔

Javascript 在angularjs中导航期间的停止时间间隔,javascript,angularjs,setinterval,Javascript,Angularjs,Setinterval,目前我正在使用angular js 在default.html页面中,我有一个get方法从服务器端获取数据 function bindActiveLoans() { $http.get(rootUrlSingleEscaped + '/cooperativa/allunfundedloans/?authId=' + userInfo.UserId) .success(function (response) {

目前我正在使用angular js

default.html
页面中,我有一个get方法从服务器端获取数据

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);
上述函数有助于每5秒钟从服务器端方法获取数据

在这里,我有一个问题

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

  • html、default3.html、default4.html、default5.html

    当我将
    default.html
    页面导航到
    default1.html
    页面时,计时器仍在运行。我只想在
    default.html
    页面中运行计时器。如果我转到另一个页面,计时器应该停止。我们如何才能做到这一点


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

    e、 g


    调用myStopFunction

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

      $scope.$on('$routeChangeStart', function (scope, next, current) {
               if(<next is not default.aspx>){
               // clear interval here
                }
      });
    
    $scope.$on(“$routeChangeStart”,函数(作用域、下一个、当前){
    if(){
    //这里有净空间隔
    }
    });
    
    来自中的angularjs示例

    使用$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);
        });
    }]);
    

    您的意思是,如果您在5个窗口/选项卡中打开了所有5个页面,那么只有一个页面应该调用计时器?如果没有,计时器会在你从一页导航到另一页时自动停止…是的,绝对正确。当我从默认页面导航到另一个页面时,我想清除默认页面中的间隔。你能给出一些示例代码吗?我想在默认页面中清除间隔如何在默认页面中写入导航事件?太好了。我想这样试试。
        var interval = $interval(function() {
            console.log('say hello');
        }, 1000);
    
        $interval.cancel(interval);
    
    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);
        });
    }]);