iOS Creating a Timer


Example

This will create a timer to call the doSomething method on self in 5 seconds.

Swift

let timer = NSTimer.scheduledTimerWithTimeInterval(5,
                               target: self,
                             selector: Selector(doSomething()),
                             userInfo: nil,
                              repeats: false)

Swift 3

 let timer = Timer.scheduledTimer(timeInterval: 1,
                                        target: self, 
                                      selector: #selector(doSomething()), 
                                      userInfo: nil, 
                                       repeats: true)

Objective-C

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(doSomething) userInfo:nil repeats:NO];

Setting repeats to false/NO indicates that we want the timer to fire only once. If we set this to true/YES, it would fire every five seconds until manually invalidated.