Home Ask Login Register

Developers Planet

Your answer is one click away!

John Hamman February 2016

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.



penatheboss February 2016

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.

Post Status

Asked in February 2016
Viewed 3,099 times
Voted 6
Answered 1 times


Leave an answer

Quote of the day: live life