Search code examples
swift2nstimercountdownstopwatch

1-2 seconds delay in NStimer countdown


after looking online for a countDown implementation in swift 2, i couldn't find anyones that works in a countdown way. so i made my own however when it reaches second 01 it takes 2 seconds to become 59. for instance if the timer is on 05:01 it takes 2 seconds of lag or timer freeze, then it becomes 4:59. it looks weird, i'm a complete beginner so my code is a disaster, here it is:

@IBOutlet var countDown: UILabel!
var currentSeconds = 59
var currentMins = 5
var timer = NSTimer()

 @IBAction func start(sender: UIButton) {
    timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(ViewController.updateTime), userInfo: nil, repeats: true)

}

 func updateTime() {

    if (currentSeconds > 9)   {
        countDown.text = "0\(currentMins):\(currentSeconds)"
        currentSeconds -= 1
    } else if ( currentSeconds > 0) && (currentSeconds <= 9) {
        countDown.text = "0\(currentMins):0\(currentSeconds)"
        currentSeconds -= 1
    } else {
        currentMins -= 1
        currentSeconds = 59
    }

    if (currentSeconds == 0) && (currentMins == 0) {
        countDown.text = "time is up!"
        timer.invalidate()
    }



}

 @IBAction func stop(sender: AnyObject) {
  timer.invalidate()
 }

Solution

  • Because you forgot to update the label:

    if (currentSeconds > 9)   {
        countDown.text = "0\(currentMins):\(currentSeconds)"
        currentSeconds -= 1
    } else if ( currentSeconds > 0) && (currentSeconds <= 9) {
        countDown.text = "0\(currentMins):0\(currentSeconds)"
        currentSeconds -= 1
    } else {
        countDown.text = "0\(currentMins):00" // <-- missed this
        currentMins -= 1
        currentSeconds = 59
    }
    

    However, it'd be better if you use NSDateFormatter to format the number of seconds left instead of managing 2 separate variables:

    class ViewController: UIViewController, UITextFieldDelegate {
        var secondsLeft: NSTimeInterval = 359
        var formatter = NSDateFormatter()
    
        override func viewDidLoad() {
            super.viewDidLoad()
            timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(ViewController.updateTime), userInfo: nil, repeats: true)
    
            formatter.dateFormat = "mm:ss"
            formatter.timeZone = NSTimeZone(abbreviation: "UTC")!
        }
    
        func updateTime()
        {
            countDown.text = formatter.stringFromDate(NSDate(timeIntervalSince1970: secondsLeft))
            secondsLeft -= 1
    
            if secondsLeft == 0 {
                countDown.text = "time is up!"
                timer.invalidate()
            }
        }
    }