Reputation: 32258
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
Reputation: 9080
Does this work?
[_timer invalidate];
_timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target: self
selector: @selector(getUpdates)
userInfo:nil
repeats: YES];
Upvotes: 2