问题描述
我是新来的角(和JS),只是有点糊涂了。
I am new to Angular (and JS) and just a little confused.
我开始一个定时器:
var getOverviewMapTimer = $interval($scope.UpdateOverviewMap, UPDATE_FREQUENCY);
和,如果我的理解是, getOverviewMapTimer
是一个承诺。
and, if I understand it, getOverviewMapTimer
is a "promise".
我希望能够检查是否定时器运行和放大器;原本希望如果我永远结果 $ interval.cancel(getOverviewMapTimer);
然后 getOverviewMapTimer
是空
我可以检查这一点。
I want to be able to check if the timer is running & had excepted that if I ever$interval.cancel(getOverviewMapTimer);
then getOverviewMapTimer
would be null
and I could check for that.
这似乎不是如此。
我必须明确地摧毁承诺(咕什么是定时器的承诺已经被取消了?)。?如果是这样,如何与放大器;我会那么必须显式地将它设置为空
?
Do I have to explicitly destroy the promise (what goo is a promise to timer that has been cancelled?).? If so, how & would I then have to explicitly set it to null
?
我的认为的我应该用取消(getOverviewMapTimer);
,但我不是100%确定为 getOverviewMapTimer
还是非空之后。
I think that I should use cancel(getOverviewMapTimer);
, but am not 100% sure as getOverviewMapTimer
is still non-null afterwards.
感谢您的帮助。
推荐答案
VAR getOverviewMapTimer = $间隔(...)后;
getOverviewMapTimer
持有一个对象的引用(恰好是一个承诺)。
After var getOverviewMapTimer = $interval(...);
getOverviewMapTimer
holds a reference to an object (that happens to be a promise).
做 $ interval.cancel(getOverviewMapTimer)
通过(并取消)由 getOverviewMapTimer
变量引用的对象,但有没有办法将对象转换为空。在 getOverviewMapTimer
变量将继续持有的承诺目标,并把它设置为null的唯一途径一提的是,通过一项新的任务(即明确地将其设置为null):
Doing $interval.cancel(getOverviewMapTimer)
passes (and cancels) the object referenced by the getOverviewMapTimer
variable, but there is no way to convert an object to null. The getOverviewMapTimer
variable will continue to hold a reference to the promise object and the only way to set it to null is through a new assignment (i.e. explicitely setting it to null):
var getOverviewMapTimer = $interval(...);
...
$interval.cancel(getOverviewMapTimer);
getOverviewMapTimer = null;
高级主题:
这听起来的确是好的,有一个简单的方法,以找出是否一个区间的承诺已被取消或没有(如添加自定义取消
属性的承诺对象取消区间)时,其值设置为true。结果
角是不够冷静,是令人难以置信的灵活性和可扩展性,因此使用 ,我们能够增强的 $间隔
服务,延长其取消()
方法添加取消
属性并将其值设置为真正
取消的区间时的承诺:
It sounds indeed nice to have an easy way to find out if an interval-promise has been cancelled or not (e.g. adding a custom cancelled
property to the promise object and setting its value to true when cancelling the interval).
Angular is cool enough to be incredibly flexible and extensible, so using the concept of a Service Decorator we are able to "augment" the $interval
service, extending its cancel()
method to add a cancelled
property and setting its value to true
when cancelling an interval-promise:
/* Service Decorators can be configured in `config` blocks */
app.config(function ($provide) {
/* Register a decorator for the `$interval` service */
$provide.decorator('$interval', function ($delegate) {
/* Keep a reference to the original `cancel()` method */
var originalCancel = $delegate.cancel;
/* Define a new `cancel()` method */
$delegate.cancel = function (intervalPromise) {
/* First, call the original `cancel()` method */
var retValue = originalCancel(intervalPromise);
/* If the promise has been successfully cancelled,
* add a `cancelled` property (with value `true`) */
if (retValue && intervalPromise) {
intervalPromise.cancelled = true;
}
/* Return the value returned by the original method */
return retValue;
};
/* Return the original (but "augmented") service */
return $delegate;
});
});
现在,我们可以使用 $间隔
服务和往常一样,我们可以随时检查的间隔,承诺的取消
属性来找出它是否已被取消。
Now, we can use the $interval
service as usual and we can always check an interval-promise's cancelled
property to find out if it has been cancelled.
var getOverviewMapTimer = $interval(...);
...
console.log(!!getOverviewMapTimer.cancelled); // false
$interval.cancel(getOverviewMapTimer);
console.log(!!getOverviewMapTimer.cancelled); // true
又见这个
See, also, this short demo.
这篇关于如何获得角$间隔的状态 - 检查间隔已被取消或不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!