NSTimer
is used is several contexts. However, you have to careful when you are going to invalidate the variable. For example:
// myTimer is defined as global
myTimer = [NSTimer scheduledTimerWithTimeInterval: 5.0
target:self
selector:@selector(myTimerMethod)
userInfo:nil
repeats:YES];
//
- (void) myTimerMethod {
// do ...
}
Usually, you will use:
[myTimer invalidate];
As you can see in the code above, the timer is switched off but the variable is unset, so it is dangerous. The best way could be:
if( myTimer != nil)
{
[myTimer invalidate];
myTimer = nil;
}
or, in a global context:
if( myTimer != nil)
{
[myTimer invalidate];
myTimer = nil;
// wait 25 seconds and start timer again
myTimer = [NSTimer scheduledTimerWithTimeInterval: 25.0
target:self
selector:@selector(myTimerMethod)
userInfo:nil
repeats:YES];
}