0

If I want to add a function as fast as the timer hits 0. Do anyone have any tips? Here is the Code that I want to implement it in.

class ViewController: UIViewController {
    @IBOutlet weak var Counterlabel: UILabel!
    var Nbr = 51
    var timerValue = 52
    var timer = NSTimer()

    override func viewDidLoad() {
        super.viewDidLoad()
        let timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("Counter"), userInfo: nil, repeats: true)
    }

    @IBAction func Btn1(sender: AnyObject) {
        Nbr = timerValue - 1
        timerValue = timerValue - 1;
    }

    func Counter(){
        Nbr -= 1
        Counterlabel.text = "\(Nbr)"
    }
}
Zia
  • 14,163
  • 7
  • 37
  • 55
David Sundström
  • 95
  • 1
  • 1
  • 9

2 Answers2

0

Call a function with delay according to your timer like this,

func delay(delay:Double, closure:()->()) {
     dispatch_after(
          dispatch_time(
               DISPATCH_TIME_NOW,
               Int64(delay * Double(NSEC_PER_SEC))
          ),
     dispatch_get_main_queue(), closure)
}

delay(Double(self.Nbr)) {
    //Do your work here
}

the above function is referenced from here

Community
  • 1
  • 1
Rajat
  • 10,530
  • 2
  • 35
  • 55
0

Try this:

   var countDown = 0 

    func Counter()
  {
     countDown--;
     if countDown == 0 
    {
       // reset the timer     
    }
  }
Lamour
  • 2,794
  • 2
  • 13
  • 27