$超时继续页面点击到其他路径后运行 [英] $timeout continues to run after page click to other path
问题描述
我使用哟:角fullstack
发电机建立自己的网站。当用户注册的网站,它会发送一封激活邮件的链接。当用户点击链接,就会显示激活成功,超时去主页。然而,当在超时尚未完成并且用户点击页面中的任何其它链路,它跳转到其他页面继续运行的超时。个别秒钟后,用户将仍然移至主页。
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 the link, it will show activation successful and a timeout to go to the home page. However, when the timeout has not finished and the user clicks any other link in the page, it jumps to other page with continuing running the timeout. Severally seconds later, the user will be still moved to the home page.
$scope.countdown = 10;
$scope.onTimeout = function() {
$scope.countdown--;
timer = $timeout($scope.onTimeout, 1000);
if ($scope.countdown === 0) {
$timeout.cancel(timer);
$location.path('/');
}
};
var timer = $timeout($scope.onTimeout, 1000);
我不知道如何取消定时
当用户点击这个页面的其他链接。
I don't know how to cancel the timer
when the user clicks other links in this page.
推荐答案
您要听AngularJS使用 $ locationChangeStart
事件改变了路线:
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.
这篇关于$超时继续页面点击到其他路径后运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!