Questions tagged [nstimer]

An Objective C class for creating timer objects.

In Objective C (), you use the NSTimer class to create timer objects or, more simply, timers. A timer waits until a certain time interval has elapsed and then fires, sending a specified message to a target object. For example, you could create an NSTimer object that sends a message to a window, telling it to update itself after a certain time interval.

Timers work in conjunction with run loops. To use a timer effectively, you should be aware of how run loops operate—see NSRunLoop and Threading Programming Guide. Note in particular that run loops retain their timers, so you can release a timer after you have added it to a run loop.

A timer is not a real-time mechanism; it fires only when one of the run loop modes to which the timer has been added is running and able to check if the timer’s firing time has passed. Because of the various input sources a typical run loop manages, the effective resolution of the time interval for a timer is limited to on the order of 50-100 milliseconds. If a timer’s firing time occurs during a long callout or while the run loop is in a mode that is not monitoring the timer, the timer does not fire until the next time the run loop checks the timer. Therefore, the actual time at which the timer fires potentially can be a significant period of time after the scheduled firing time.

NSTimer is “toll-free bridged” with its Core Foundation () counterpart, CFRunLoopTimerRef. See “Toll-Free Bridging” for more information on toll-free bridging.

Additional examples for the NSTimer object can be found here.

2624 questions
6
votes
2 answers

What's the minimum valid time interval of an NSTimer?

I want to use NSTimer to increase the number which show on a label. Here is my code: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.numberLabel = [[UILabel…
无夜之星辰
  • 5,426
  • 4
  • 25
  • 48
6
votes
2 answers

Multiple timers in UITableViewCell (Swift)

I have UITableViewcells that are created at different moments in time and I would like each one of them to have an independent timer that triggers when the object is added with reloadData() This is what I have done so far import UIKit var timer =…
6
votes
1 answer

AVPlayer does not update currentPlaybackTime when seeking backwards

I have an NSTimer set up that fires every 0.1 seconds, in the callback I fetch currentTime() and use it to update the label with the duration of the video. When I am seeking forwards, by setting the rate to 3, this timer keeps up, but when I set the…
vrwim
  • 13,020
  • 13
  • 63
  • 118
6
votes
2 answers

What is an NSTimer's behavior when the app is backgrounded?

I know that when you background an app, the timer stops running. However, what is the behavior when you come back from the background? Does the timer maintain its original fireDate? I've run into a bit of an issue where upon returning from the…
Bill L
  • 2,576
  • 4
  • 28
  • 55
6
votes
2 answers

How do I find the time interval remaining from NSTimer

I have set a NSTimer.scheduledTimerWithTimeInterval method which has an interval every 20 minutes. I want to be able to find out how much time is left when the the app goes into background mode. How do I find out how much time is left from the…
Henry Brown
  • 2,219
  • 8
  • 31
  • 48
6
votes
4 answers

Swift countdown timer labels for Days/Hours/Minutes/Seconds

I'm creating a countdown timer that counts down to an NSDate set in a UIDatePicker. I have a label that shows the date we're counting down to and that works fine. What I'm also trying to add is labels for the number of whole days left and number of…
Chris Byatt
  • 3,689
  • 3
  • 34
  • 61
6
votes
4 answers

Continue countdown timer when app is running in background/suspended

I have a functional countdown timer. The problem is that I need to continue the countdown when the user puts the app in the background. Or suspends the app? I am not sure what the correct terminology is to describe this action. In other words, the…
Michael Ninh
  • 772
  • 2
  • 10
  • 23
6
votes
2 answers

Format realtime stopwatch timer to the hundredth using Swift

I have an app using an NSTimer at centisecond (0.01 second) update intervals to display a running stopwatch in String Format as 00:00.00 (mm:ss.SS). (Basically cloning the iOS built-in stopwatch to integrate into realtime sports timing math…
mothy
  • 147
  • 2
  • 8
6
votes
1 answer

Calling a static method from NSTimer. Is it possible?

Is it allowed to call a static method from an NSTimer? The compiler will not allow this, complaining with the cryptic "Extra argument 'selector' in call. struct MyStruct { static func startTimer() { …
Luke Bartolomeo
  • 551
  • 7
  • 11
6
votes
4 answers

Swift how to use NSTimer background?

class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() var timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("update"), userInfo: nil, repeats: true) …
David Wang
  • 934
  • 1
  • 12
  • 16
6
votes
4 answers

iphone NStimer start in 2 seconds

I am trying to have a block of code run 2 seconds after I 'start' it. I think the NSTimer can do this but can't figure it out.
Ian Vink
  • 66,960
  • 104
  • 341
  • 555
6
votes
2 answers

How to kill NSTimer after entering background and create a new timer after the app is back to active?

So I am building an simple iOS app using Swift. I need to kill my NSTimer after the app entering background, and create a new one after the app is active again. Initially my solution is to create a timer of NSTimer class in ViewDidLoad() in the…
Antimony
  • 143
  • 1
  • 9
6
votes
3 answers

NSTimer.scheduledTimerWithTimeInterval in Swift Playground

All the examples I've seen on using the "NSTimer.scheduledTimerWithTimeInterval" within Swift show using the "target: self" parameter, but unfortunately this doesn't work in Swift Playgrounds directly. Playground execution failed: :42:13:…
Chris Pietschmann
  • 29,502
  • 35
  • 121
  • 166
6
votes
4 answers

Disable a Button for 90 sec when pressed in swift

I have button that show up a modal view but i want that if the user click it he wont be able to use it again for 90 seconds. how can i do this?
Ludyem
  • 1,709
  • 1
  • 18
  • 33
6
votes
0 answers

Changing Updating Frequency of CMAltimeter

I am working on an app that uses the CMAltimeter class. I would like to retrieve altitude values quicker than the default value (default seems to be about once every second). How can I change the frequency? Will I need to use NSTimers/Multiple…
user3647894
  • 559
  • 1
  • 4
  • 28