Nick
Nick

Reputation: 555

How to increase speed of timer object using Swift

I am using an NSTimer.scheduledTimerWithTimeInterval object but cannot increase the speed of the clock to faster than 1/1000. I have tried invalidating the clock and re-creating. timer2 & timer3 work fine on the iOS simulator but does not work when testing on iPhone 5s device. Speed stays always on 1/1000 even when firing timer2 or timer3. Below is my definition:

timer = NSTimer.scheduledTimerWithTimeInterval(0.001, target: self, selector: Selector("result"), userInfo: nil, repeats: true)

timer2 = NSTimer.scheduledTimerWithTimeInterval(0.002, target: self, selector: Selector("result"), userInfo: nil, repeats: true)

timer3 = NSTimer.scheduledTimerWithTimeInterval(0.003, target: self, selector: Selector("result"), userInfo: nil, repeats: true)

Upvotes: 0

Views: 1984

Answers (1)

matt
matt

Reputation: 535138

You cannot change a timer. If you want a timer running at a different interval, you have to invalidate and destroy the existing timer and replace it with a new one. That's quite a common thing to do; timers are lightweight objects.

Upvotes: 1

Related Questions