How to Find the Time Interval Remaining from Nstimer

How do I find the time interval remaining from NSTimer

You have access to a NSTimer's fireDate, which tells you when the timer is going to fire again.

The difference between now and the fireDate is an interval you can calculate using NSDate's timeIntervalSinceDate API.

E.G. something like:

let fireDate = yourTimer.fireDate
let nowDate = NSDate()
let remainingTimeInterval = nowDate.timeIntervalSinceDate(fireDate)

How to get time remaining on an NSTimer as a double

timer.fireDate.timeIntervalSinceNow should return the number of seconds remaining until the timer fires as an NSTimeInterval which is a Double

Getting the time remaining in the time interval of a Timer in Swift

Do

let timeRemaining = shootingEngine.fireDate.timeIntervalSince(Date())

Let me know if you have any other questions. Good luck!

Swift 4 Get current timeInterval of Scheduled Timer

You can use "contentTimer.fireDate.timeIntervalSince(Date())" to get the current timeinterval.

How does one change/update the time interval in an NSTimer?

You can't. Invalidate the timer, and create a new one with a different time interval.

You can change the fire time. So if you want to change the time interval after the first firing only, create the timer with that time interval, then change the firing time so that the timer is fired the first time at the time you want.

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

The time period of an NSTimer is a value of type NSTimeInterval, while this provides sub-millisecond precision that does not help you. From the start of the NSTimer documentation:

Timers work in conjunction with run loops. Run loops maintain strong references to their timers, so you don’t have to maintain your own strong reference to a timer after you have added it to a run loop.

To use a timer effectively, you should be aware of how run loops operate. See Threading Programming Guide for more information.

A timer is not a real-time mechanism. If a timer’s firing time occurs during a long run loop callout or while the run loop is in a mode that isn't monitoring the timer, the timer doesn't fire until the next time the run loop checks the timer. Therefore, the actual time at which a timer fires can be significantly later. See also Timer Tolerance.

So the minimum time interval for an NSTimer is tied to the the length of a run loop iteration. While internal optimisations, if they exist, could fire a timer as soon as it is set if the interval is really small in general the shortest period you'll get is dependent on the remaining execution time of the run loop iteration in which the timer is set, which is pretty much indeterminate for general purpose programming.

If you really need a high-resolution timer (see @bbum's comment on your question) then you'll need to research that topic - just search something like "high resolution timing macOS" as a starting point.

HTH

How can I use Timer (formerly NSTimer) in Swift?

This will work:

override func viewDidLoad() {
super.viewDidLoad()
// Swift block syntax (iOS 10+)
let timer = Timer(timeInterval: 0.4, repeats: true) { _ in print("Done!") }
// Swift >=3 selector syntax
let timer = Timer.scheduledTimer(timeInterval: 0.4, target: self, selector: #selector(self.update), userInfo: nil, repeats: true)
// Swift 2.2 selector syntax
let timer = NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: #selector(MyClass.update), userInfo: nil, repeats: true)
// Swift <2.2 selector syntax
let timer = NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: "update", userInfo: nil, repeats: true)
}

// must be internal or public.
@objc func update() {
// Something cool
}

For Swift 4, the method of which you want to get the selector must be exposed to Objective-C, thus @objc attribute must be added to the method declaration.



Related Topics



Leave a reply



Submit