超过$ count参数后,角度$ interval是否会自行取消? [英] Does the angular $interval cancel itself after exceeding 'count' parameter?

查看:77
本文介绍了超过$ count参数后,角度$ interval是否会自行取消?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关$ interval服务的快速提问(角度).在查看文档( $ interval )时,他们警告您手动取消间隔,但是您可以选择在初始化时提供一个count参数.计时器一旦超过"分配的计数,它会自行取消还是只是停止调用该函数并在后台运行?

Quick question about the $interval service in angular. Looking at the docs ($interval) they warn you to manually cancel intervals, but you have the option of providing a count parameter upon initialization. Once the timer has "ticked" past the allotted count does it cancel itself or simply stop calling the function and live on in the background?

推荐答案

TL; DR;计数后,间隔将被清除.

正如同一文档所述,建议您在破坏控制器范围时取消$ interval.像这样:

TL;DR; After the count, the interval is cleared.

As the same documentation says, it is recommended that you cancel the $interval when the scope of your controller is detroyed. Something like:

var t = $interval(function(){
    ...
}, 1000);


$scope.$on('$destroy', function(){
    $interval.cancel(t);
});

delay参数是调用该函数的时间间隔.在上面的示例中,该函数每1000毫秒调用一次.如果您不取消$ interval,Angular将保留对其的引用,并可能继续执行您的功能,从而在您的应用中引起奇怪的行为.

The delay parameter is time interval which the function is called. In the example above, the function is called every 1000 milliseconds. If you don't cancel the $interval, Angular will hold a reference to it, and may continue to execute your function, causing strange behaviors in your app.

考虑到$ interval提供程序只是本机setInterval()的包装,加上$ apply,请查看Angular实现( https://github.com/angular/angular.js/blob/master/src/ng/interval.js ),我们可以找到以下代码段:

Considering that the $interval provider is just a wrapper of the native setInterval(), with the adition of the $apply, looking at the Angular implementation (https://github.com/angular/angular.js/blob/master/src/ng/interval.js), we can find this snippet of code:

if (count > 0 && iteration >= count) {
  deferred.resolve(iteration);
  clearInterval(promise.$$intervalId);
  delete intervals[promise.$$intervalId];
}

因此,解决了提供商创建的承诺,并且清除了间隔. cancel方法执行此操作:

So, the promise created by the provider is resolved, and the interval is cleared. The cancel method does this:

intervals[promise.$$intervalId].reject('canceled');
$window.clearInterval(promise.$$intervalId);
delete intervals[promise.$$intervalId];

所以,我认为您的假设是正确的.计数后,间隔已清除.

So, I think your assumption is right. After the count, the interval is already cleared.

这篇关于超过$ count参数后,角度$ interval是否会自行取消?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆