iOS NSTimer Creating a Timer

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

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.



Got any iOS Question?