countdown timer button swift stack overflow code example
Example 1: how do i have countdown timer in swift stackoverflow
@IBAction func start(sender: UIButton) {
self.timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(ViewController.update(_:)), userInfo: nil, repeats: true)
NSRunLoop.currentRunLoop().addTimer(self.timer, forMode: NSRunLoopCommonModes)
startTime = NSDate()
}
func update() {
let elapsedTime = NSDate().timeIntervalSinceDate(startTime)
let currTime = totalTime - elapsedTime
countDown.text = String(currTime)
if currTime < 0 {
timer.invalidate()
}
}
Example 2: timer in swift stack overflow
var timer = NSTimer()
timer(timeInterval: 0.01, target: self, selector: update, userInfo: nil, repeats: false)