George Johnston
George Johnston

Reputation: 32258

How can I reset a timer?

I have an app, that every 3 seconds, fires an event that goes to my server and grabs information. I would like to reset the timer count down if an event has transpired in between the time the timer last fired and when it fires subsequently.

So essentially, if my other event fires at 2.5 seconds, and the timer is set to fire in .5 seconds, I would like to reset the timer back to 3 seconds. How can I accomplish this?

I declare the timer as:

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

And psuedo:

-(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.
}

Upvotes: 1

Views: 270

Answers (1)

Simon Goldeen
Simon Goldeen

Reputation: 9080

Does this work?

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

Upvotes: 2

Related Questions