gpt4 book ai didi

ios - 无法停止 NSTimer

转载 作者:行者123 更新时间:2023-11-28 19:01:14 25 4
gpt4 key购买 nike

我用这段代码来停止 NSTimer

[timer invalidate]
timer = nil;

它在第一次运行时运行良好。但是,在我使用这段代码恢复计时器之后。

 timer = [NSTimer scheduledTimerWithTimeInterval:5.0
target:self
selector:@selector(checkNewUpdates)
userInfo:nil
repeats:YES];

NSTimer 不会再因 [timer invalidate]

而停止

最佳答案

看起来多个计时器实例同时运行。您可以做一件事,在开始运行新计时器之前,检查以前的计时器实例,如果计时器实例可用,则使它无效。在此之后启动新实例

if(timer)
{
[timer invalidate];
timer = nil;
}
timer = [NSTimer scheduledTimerWithTimeInterval:5.0
target:self
selector:@selector(checkNewUpdates)
userInfo:nil
repeats:YES];

关于ios - 无法停止 NSTimer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25299341/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com