如何重置计时器?

时间:2011-04-20 20:13:45

标签: cocoa-touch

我有一个应用程序,每隔3秒,会触发一个事件进入我的服务器并抓取信息。如果事件在计时器最后一次触发和随后触发之间发生,我想重置计时器倒计时。

基本上,如果我的其他事件在2.5秒时触发,并且计时器设置为在0.5秒内触发,我想将计时器重置为3秒。我怎么能做到这一点?

我将计时器声明为:

_timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target: self
                                                selector: @selector(getUpdates)
                                                userInfo:nil
                                                repeats: YES];

并伪装:

-(void) anEventHappened
{
  // I got something from the server, I don't need to query it.  Reset timer here.
}

-(void) getUpdates
{
  // I received no reset, I should check for an update.
}

1 个答案:

答案 0 :(得分:2)

这有用吗?

[_timer invalidate];
_timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target: self
                                            selector: @selector(getUpdates)
                                            userInfo:nil
                                            repeats: YES];