-
Notifications
You must be signed in to change notification settings - Fork 472
Using Timers
Timothy Lee edited this page Jun 18, 2018
·
7 revisions
Configure a simple repeating timer as in the snippet below. After calling the scheduledTimerWithTimeInterval method, the timer is immediately scheduled and will call the function at the time interval.
Declare and implement the method that you want to be called when the timer fires. You can name this method whatever you want, just pass the name of the method to the timer when you create it.
func onTimer() {
// Add code to be run periodically
}
- (void)onTimer {
// Add code to be run periodically
}
For example, to schedule the onTimer
method to be called every 5 seconds, use the code below. If you want the timer to fire every 100ms, just use 0.1 for the time interval.
Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(self.onTimer), userInfo: nil, repeats: true)
[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(onTimer) userInfo:nil repeats:true];