如何判断 NSTimer 是否处于活动状态?

发布于 2024-08-08 17:25:44 字数 461 浏览 3 评论 0原文

我有一个类似这样的计时器:

 NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                                   target:self
                                                 selector:@selector(updateCountdown)
                                                 userInfo:nil
                                                  repeats:YES]; 

我正在使用这个计时器更新标签的文本。在某种情况下,我想检查计时器是否处于活动状态,然后使计时器无效。我的问题是如何知道计时器是否处于活动状态?

I have a timer something like this:

 NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                                   target:self
                                                 selector:@selector(updateCountdown)
                                                 userInfo:nil
                                                  repeats:YES]; 

I am updating a label's text using this timer. At a certain condition, I want to check if the timer is active then invalidate the timer. My question is how do I find that timer is active or not?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

旧人 2024-08-15 17:25:44

当非重复计时器触发时,它会将自己标记为无效,因此您可以在取消它之前检查它是否仍然有效(当然,然后摆脱它)。

if ( [timer isValid] && yourOtherCondition){
    [timer invalidate], timer=nil;
}

在您的情况下,您有一个重复计时器,因此它始终有效,直到您采取某些操作使其无效。看起来在这种情况下您正在运行倒计时,因此当倒计时达到所需值时,您需要确保无效并摆脱它(在您的 updateCountdown 方法中)

When a non repeating timer fires it marks itself as invalid so you can check whether it is still valid before cancelling it (and of course then ridding yourself of it).

if ( [timer isValid] && yourOtherCondition){
    [timer invalidate], timer=nil;
}

In your case you have a repeating timer so it will always be valid until you take some action to invalidate it. Looks like in this case you are running a countdown so it will be up to you to make sure you invalidate and rid yourself of it when the countdown reaches the desired value (In your updateCountdown method)

天涯沦落人 2024-08-15 17:25:44

NSTimer 有一个 -isValid 方法。

NSTimer has an -isValid method.

久隐师 2024-08-15 17:25:44

将计时器保存在实例变量中,并在没有计时器运行时(即在调用[timer invalidate]之后)设置timer = nil。然后,要检查计时器是否处于活动状态,您只需检查是否 timer == nil 即可。

Keep the timer in an instance variable, and set timer = nil when there's no timer running (i.e. after you call [timer invalidate]). Then, to check if the timer is active, you can just check whether timer == nil.

凹づ凸ル 2024-08-15 17:25:44

在 Swift 中,您可以使用 isValid 布尔值来查看计时器是否正在运行:

if timer.isValid {
   // Do stuff
}

来自 Apple 文档

一个布尔值,指示接收者当前是否在
有效的。 (只读)

如果接收器仍然能够发射,则为 true;如果计时器已关闭,则为 false
无效并且不再能够发射。

In Swift, you can use the isValid boolean to see if the timer is running:

if timer.isValid {
   // Do stuff
}

From the Apple docs:

A Boolean value that indicates whether the receiver is currently
valid. (read-only)

true if the receiver is still capable of firing or false if the timer has been
invalidated and is no longer capable of firing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文