将NSTimer的发射时间重置为从现在起,而不是最后的火 [英] Reset an NSTimer's firing time to be from now instead of the last fire
问题描述
我有一个 NSTimer
,以3秒的间隔触发,以减少一个值。当我做一个动作增加那个值,我想重新启动计时器从这一点计数3秒。
I have a NSTimer
that fires with an interval of 3 seconds to decrease a value. When I do an action that increases that value, I want to restart the timer to count 3 seconds from that point.
例如,如果我增加的值,火灾在1秒钟,我想改变,使定时器火在3秒,而不是。我可以使计时器无效并再次创建吗?或者我可以使用 setFireDate:
使用当前日期并添加3秒的间隔吗?
For example, if I increase the value and timer will fires in 1 second, I want to change that and make the timer fire in 3 seconds instead. Can I invalidate the timer and create it again? Or can I do it with setFireDate:
, using the current date and adding an interval of 3 seconds?
推荐答案
是的,您可以使其失效。并再次创建它。
Yes, you can invalidate it. And create it again.
您也可以使用:
- (void) myTimedTask {
// other stuff this task needs to do
// change the variable varyingDelay to be 1s or 3s or... This can be an instance variable or global that is changed by other parts of your app
// weStillWantTimer is also a similar variable that your app uses to stop this recurring task
if (weStillWantTimer)
[self performSelector:@selector(myTimedTask:) withObject:gameColor afterDelay:varyingDelay];
}
您调用myTimedTask来启动重复任务。启动后,您可以使用varyDelay更改延迟或使用weStillWantTimer停止它。
You call myTimedTask to start the recurring task. Once it is started, you can change the delay with varyingDelay or stop it with weStillWantTimer.
这篇关于将NSTimer的发射时间重置为从现在起,而不是最后的火的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!