Javascript 在 routeChange 上取消 AngularJS $timeout

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14897608/
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-08-24 18:15:39  来源:igfitidea点击:

Cancel a AngularJS $timeout on routeChange

javascripttimeoutangularjs

提问by Rasmus

On a specific page in my application I think of doing a server-call to update information on a set interval. I stumbled upon a problem though. I want to cancel my $timeout when a user navigates away from the page in question so that the application doesn't try to work with stuff that isn't there anymore.

在我的应用程序中的特定页面上,我考虑进行服务器调用以在设定的时间间隔内更新信息。我偶然发现了一个问题。当用户离开有问题的页面时,我想取消我的 $timeout,这样应用程序就不会尝试处理不再存在的内容。

Any ideas on how to work around this?

关于如何解决这个问题的任何想法?

回答by EpokK

Use $timeout.cancellike this:

$timeout.cancel像这样使用:

yourTimer = $timeout(function() { /* ... */ }, 5000);
$timeout.cancel(yourTimer);

Reference

参考