$timeout continues to run after page click to other path

后端 未结 2 952
甜味超标
甜味超标 2021-01-29 01:17

I am using yo:angular-fullstack generator to build my website. When a user registers to the site, it will send an activation email with a link. When a user clicks t

相关标签:
2条回答
  • 2021-01-29 01:39

    You need to listen to AngularJS changing the route using the $locationChangeStart event:

    $scope.$on('$locationChangeStart', function () {
        $timeout.cancel(timer);
    });
    

    That way, when the route changes, the timer is cancelled and the user is not redirected.

    0 讨论(0)
  • 2021-01-29 01:41
    module.controller("TestController", function($scope, $timeout) {
    
        var onTimeout = function() {
            // something
        };
        var timer = $timeout(onTimeout, 1000);
    
        $scope.$on("$destroy", function() {
            if (timer) {
                $timeout.cancel(timer);
            }
        });
    });
    

    you can call it simply when scope is destroyed.

    0 讨论(0)
提交回复
热议问题