- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我有一个计时器,它是 不是 重复的。每次触发时,正在执行的方法会根据我的应用程序的某些内部逻辑决定是否重新安排它。
此方法可从应用程序的其他部分使用,因此我在该方法中所做的第一件事是检查计时器是否仍然有效(了解发起者是计时器还是其他实体),以防万一不是由我想让它失效的计时器启动的:
if (self.pollingTimer.isValid) {
[self.pollingTimer invalidate];
self.pollingTimer = nil;
}
isValid
收到一个真实值属性,即使查看
NSTimer
scheduledTimerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats
下的文档方法:
repeats
If YES, the timer will repeatedly reschedule itself until invalidated. If NO, the timer will be invalidated after it fires.Discussion
After seconds seconds have elapsed, the timer fires, sending the message aSelector to target.
最佳答案
定时器不是实时机制;它仅在已添加计时器的运行循环模式之一正在运行并且能够检查计时器的触发时间是否已过时触发。因此,计时器不会立即使自己失效,而是在运行循环结束时。
作为一个简单的测试,您可以看到:
- (void)viewDidLoad {
[super viewDidLoad];
_timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(timerFired) userInfo:nil repeats:NO];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
if (self.timer.isValid){
NSLog(@"--> TIMER VALID");
} else {
NSLog(@"TIMER INVALID!");
}
});
}
- (void) timerFired {
if (self.timer.isValid){
NSLog(@"--> TIMER VALID");
} else {
NSLog(@"TIMER INVALID!");
}
}
这将记录
--> TIMER VALID
来自
timerFired
方法和来自
dispatch_after
的 block 时被调用,你会看到
TIMER INVALID!
.因此,当您使用
repeats:NO
安排计时器时, 它
保证不重新安排自己,但它不会立即失效。
repeats
If YES, the timer will repeatedly reschedule itself untilinvalidated. If NO, the timer will be invalidated after it fires (but not immediately)
关于ios - NSTimer 的 isValid 总是返回 YES,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30614564/
注意:我在这个项目中使用 swift。 我目前正在开发我的第一个项目,如果在一段时间后没有按下按钮,我会尝试停止(无效)NSTimer。我找到了其他停止 NSTimer 的方法,但没有说明如何在一定时
我正在 Xcode4 中工作,在一个项目中,出于组织目的,我一直使用三个不同的 NSTimer,但我意识到,除了名称之外,计时器是相同的,因此很容易将所有三个计时器合并为一。对于我的项目来说,哪一个更
根据 Apple 文档,将同时使用 WKInterfaceTimer(Watch 本地,倒计时但在结束时不触发任何事件)和 NSTimer(在计时器结束时触发方法)。所以,我的 App Interfa
我目前正在尝试制作一个 queueHandler,它将一个对象数组作为输入,用于在一个简单的 Double 机器人上执行驱动命令。我目前正在尝试使用 GCD 以串行执行我的函数,但是当我在我的队列中使
有个问题...我有定时器 [NSTimer scheduledTimerWithTimeInterval:120
我创建了一个计时器, self.scanTimer = [NSTimer scheduledTimerWithTimeInterval:TIMEOUT_SECONDS target:self s
我尝试使用以下代码停止 NSTimer: - (void)viewDidLoad { [super viewDidLoad]; timer3 = [NSTimer timerWithT
我正在制作一个带有计时器的应用程序。我从给定时间开始计算分钟和秒,直到 0。发生这种情况时,我启动一个警报 View 。 我的结构是这样的: 主线程方法分配一个新线程并初始化它。线程的入口点(方法)有
好吧,这段代码非常基本。用户将答案输入文本框,如果等于“第一+第二”,他们就得到一分。然后,他们有 5 秒钟的时间回答下一个数学问题。如果他们这样做了,函数“doCalculation”将再次运行,他
我正在创建益智游戏应用程序,并使用 NSTimer 显示时间(即 01:20)。当应用程序进入后台时,NSTimer 会暂停,但即使应用程序处于后台状态,我也想继续它。 例如,当应用程序进入后台时,计
我有一个带有 NSTimer *myTimer; 变量的类。在某些时候我会这样做: myTimer = [NSTimer scheduledTimerWithTimeInterval:20 targe
Edit2:为什么在“doSomething”方法中只更新了进度而不是point0? 编辑:使用我拥有的代码。我知道我一定忽略了一些东西,但我就是找不到它。 我正在编写一个 iPhone 应用程序,它
我的应用是一款体育应用。即使在后台游戏也应该继续,我为此使用了 nstimer。但我无法在后台运行计时器。每当应用程序处于后台时,计时器就会停止,返回时它会从离开时的同一时间继续。 最佳答案 您无法在
它将不断倒计时并更新当前时间。任何人都知道如何解决这个问题?我不明白 NStimer 如何识别这样的日期格式:20110803 23:59:59 最佳答案 查找NSDateFormatter 。您可以
是否可以在主线程上运行由 nstimer 调用的选择器? NSTimer 在它自己的线程中生成。 我的结构是一个线程调用一个带有 nstimer 的方法,nstimer 调用一个执行一些更新的方法,但
我在我的应用程序中使用了很多计时器。用于记录时间、移动物体、淡入淡出等。我在不同时间、同一 View 中使用同一个计时器来实现多个目的。我应该如何正确声明和无效或释放我的计时器? Atm 我这样声明计
我正在尝试使用 iPhone SDK 3.0 在线程上运行 NSTimer。我认为我做的一切都是正确的(新的运行循环等)。如果我在 viewDidDissappear 上调用 [timer inval
这是让我困惑了一段时间的事情。 我有一个 NSTimer,添加到 currentRunLoop 中,如果我不保留它,它就会崩溃。 NSTimer *timer = [[NSTimer timerWit
当我们使用 NSTimer 时,一旦在上述时间间隔后调用回调,UI 是否会被阻塞? 最佳答案 这要看情况。大多数时候,这不会成为问题。 如果,但是,满足以下两个条件,NSTimer 将阻塞 UI 线程
我使用大约 20 个 UIImageView 的小型飞机图像(50x50 像素)在 iPhone 屏幕上制作简单的动画。动画是通过按计时器间隔移动 UIImageView center 属性来完成的。
我是一名优秀的程序员,十分优秀!