Install NSTimer to run once in the future

How to configure NSTimer to run once in the future (say, 30 seconds). So far, I have managed to install it so that it works immediately, and then at intervals.

+7
source share
3 answers

The method you want to use:

+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval) seconds target:(id) target selector:(SEL) aSelector userInfo:(id) userInfo repeats:(BOOL) repeats 

with arguments repeats == NO and seconds == 30 . This will create a timer and schedule it. It works only once, after 30 seconds (and not immediately).

+13
source

You can set a timer with your future date and set the retries to NO

 + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval) seconds target:(id) target selector:(SEL) aSelector userInfo:(id) userInfo repeats:(BOOL) repeats 
+6
source

Use this class method to schedule a timer.

  +(NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats 

Options
seconds
The number of seconds between timer shooting. If seconds are less than or equal to 0.0, this method selects a non-negative value of 0.1 milliseconds instead.
target
The object to send the message specified by aSelector when the timer fires. The target is saved by the timer and freed when the timer is invalid.
aSelector
The message sent to the target when the timer fires. The selector must have the following signature:
- (void) timerFireMethod: (NSTimer *) theTimer
The timer passes itself as an argument to this method.
USERINFO
User information for the timer. The specified object is saved by the timer and released when the timer is invalid. This parameter may be zero.
repeats
If YES, the timer will redirect itself until it is canceled. If NO, the timer will be invalid after it occurs.
Example

  [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(targetMethod:) userInfo:[self userInfo] repeats:NO]; 

The timer starts automatically after 2 seconds. Timer Programming Topics

+6
source

All Articles