How to stop the NStimer event?

Possible duplicate:
NSTimer does not stop

In my application, I use NStimer to call an animation function every 3 seconds. I want to stop this timer and trigger another event while the timer is still running. Is it possible?

+46
ios objective-c iphone nstimer
Sep 15 '09 at 20:56
source share
2 answers
@interface NSTimer *autoTimer; @implementation // Start timer autoTimer = [NSTimer scheduledTimerWithTimeInterval:(3.0) target:self selector:@selector(autoTimerFired:) userInfo:nil repeats:YES]; // Stop timer: [autoTimer invalidate]; autoTimer = nil; 
+101
Sep 15 '09 at 21:27
source share

First, you want to keep a pointer to a timer

 self.packetTimer = [NSTimer timerWithTimeInterval:CONNECTION_TIMEOUT target:self selector:@selector(connectionTimeout:) userInfo:nil repeats:NO]; [[NSRunLoop currentRunLoop] addTimer:packetTimer forMode:NSDefaultRunLoopMode]; 

If you want to cancel it elsewhere in your code, just call:

 [self.packetTimer invalidate]; self.packetTimer = nil; 
+42
Sep 15 '09 at 21:15
source share



All Articles