简体   繁体   中英

Subsequent WKInterfaceTime / NSTimer events with array of NSTimeInterval values

I am trying to run subsequent Timer events with WKInterfaceTimer & NSTimer, the problem is that I cannot figure out a way to make more than two subsequent calls with one NSTimer object. Basically, I would like run timer to complete then fire up the next.

Here's some sample code that hopefully explains my idea a little better....

1) I am firing off the first timer in awakeWithContext:

func initalTimer() {

    let timer1String = NSMutableAttributedString(string: "Lap1")

    runStatusLabel.setAttributedText(timerString) 

    myTimer = NSTimer.scheduledTimerWithTimeInterval(duration, target: self, selector: Selector("timerDone"), userInfo: nil, repeats: false)
    runTimer.setDate(NSDate(timeIntervalSinceNow: duration))
    runTimer.start()        
}

NOTE: Everything works great at this point, then the tiemrDone function is called where I then fire off another timed event.

2)

func timerDone() {

    //print("Done")
    elapsedTime = 0.0

    myTimer!.invalidate()
    startTime = NSDate()
    timeRunning = false

    // Call second timed event 
    timer2()  // just another NSTimer / WKInterfaceTimer function
}

"Stacking" the functions with a completionHandler does not seem to help OR most likely I am doing something wrong...

func execute_Timers(timeInterval: NSTimeInterval, completionHandler: (success:        Bool, error: String?) -> Void  ) -> Int   {

 // Code below never gets executed 
}

I haven't tested this, and it is just a guess: When your timerDone() method is called, you invalidate the timer. Therefore it doesn't "complete," so your completion routine isn't called. When your timer completes, it gets invalidated anyway, so the call should not be needed. Try removing:

 myTimer!.invalidate()

and see what happens.

Thanks for the reply, and you are quite correct - I do not need to call myTimer!.invalidate(). The solution that worked for me was to have different timerDone methods and conditionaly call the next time method.

-Paul

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM