简体   繁体   中英

Swift 2 Should I use 2 NSTimers or 1 for a delayed start timer

I am making an iOS app in Swift 2 that the user will select a delay (from slider) then the length of the timer. When the user clicks start, the timer will wait for the delay time and then start (by signaling a beep). When it reaches the end of the timer length, it will stop and signal with a beep.

Is it better to use one or two timers. Meaning, should I create a "Delay" timer that when it reaches the end of the delay, it starts the actual timer. OR, should I use one timer that beeps at the end of the delay time, starts the screen clock, and then beeps and terminates the timer at the end of the official timer.

John

You only need a single timer:

Declare this within the class:

var timer = NSTimer()

Put this in the function where you start the delay:

timer = NSTimer.scheduledTimerWithTimeInterval(DELAYTIME, target: self, selector: "delayTime", userInfo: nil, repeats: false)

And you will need 2 functions:

func delayTime() {
     timer = NSTimer.scheduledTimerWithTimeInterval(TIMERSPEED, target: self, selector: "useTimer", userInfo: nil, repeats: true)
}

func useTimer() {
    //Do whatever you want!
}

Hope this helps.

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