Swift. Get how many seconds / milliseconds have passed since the start of the timer.

trying to figure out how to get the elapsed time since the timer started. this is what i did: Feel free to suggest other ways to get the time elapsed after page load

Timer declared:

 var runner = NSTimer()
 runner = NSTimer.scheduledTimerWithTimeInterval(0.6, target: self, selector: Selector("time"),      userInfo: nil, repeats: true)

      

Than tried this in a function, returns the interval i (obviously)

  func time() {

        println(self.runner.timeInterval)
    }

      

anyway if you get how much time has passed since it started? (he's in the didload section, so he likes to say how much time has passed since the page was loaded). thank you!: D

+3


source to share


1 answer


Place it on the pad. I added a loop to create a time delay. Time in seconds to a fraction of a second.

var startTime = NSDate.timeIntervalSinceReferenceDate()
var currentTime: NSTimeInterval = 0

for var i = 0; i < 10000; i++ {
    if i == 99 {
        currentTime = NSDate.timeIntervalSinceReferenceDate()
        println(i)
    }

}
var elapsedTime = currentTime - startTime
println(Double(elapsedTime))

      

From: https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/#//apple_ref/occ/instp/NSDate/timeIntervalSinceReferenceDate



timeIntervalSinceReferenceDate

      

Returns the interval between a date object and January 1, 2001 at 12:00 pm clockwise. (in seconds)

The method sets startTime when you want to start. This is the time in seconds since the link. Later, we set currentTime to the new time since the start. Subtracting one from the other gives the elapsed time. There are several decimal places of precision.

+4


source







All Articles