0

I'm working on an a timer app which takes in two variables as times for the timer periodOneTime and periodTwoTime. Once the time is up on one periodOneTime, I want my app to switch to the periodTwoTime, and back and forth for my timer.

The variable timeLeft under the function secondTimer is holding these variables to check if the timer is up, and I need a way to check if the time on periodOneTime and periodTwoTime is up so I can switch to the next time. I've no idea where to start on this. Thanks for any help!

  @IBAction func startTimer(_ sender: UIButton) {
    periodOneTime = Int(periodOne.text!)!
    periodTwoTime = Int(periodTwo.text!)!

    timeLeft = periodOneTime

    timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(onTimerFires), userInfo: nil, repeats: true)
}

@IBAction func stopTimer(_ sender: UIButton) {
    timer?.invalidate()
    timeRemaining.text = "0"
}

@objc func onTimerFires()
{
    timeLeft -= 1
    timeRemaining.text = String(timeLeft)

    if timeLeft <= 0 {
        timer?.invalidate()
        timer = nil
    }
    //checks if time left is 0, then stops and starts second timer from periodTwo
    if timeLeft == 0 {
        timer?.invalidate()
        secondTimer()
    }
}
//Function to switch to second timer (periodTwo)
func secondTimer(){

    if timeLeft < 1{
        timeLeft = periodTwoTime
          timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(onTimerFires), userInfo: nil, repeats: true)
        }

}
Alain Stulz
  • 715
  • 6
  • 19
RyanHaraki
  • 63
  • 1
  • 9
  • why? What are you trying to achieve with the two timers? If the first timer is just so that you can update a display, while the second one is the 'real' timer, this has been demonstrated lots of places - like this https://stackoverflow.com/questions/29374553/how-can-i-make-a-countdown-with-nstimer – Russell Apr 14 '20 at 07:44
  • My dad wants a running app where he can set a time for himself to run, and once that time elapses he wants the second timer to run so he has a certain amount of time to walk, and back and forth. I am building the app for him. – RyanHaraki Apr 14 '20 at 18:15
  • Ok, makes sense - and you’re going about it the right way I think - when the first timer finishes use that to trigger the next one. If you want a timer display, you need another one running to drive the update. – Russell Apr 14 '20 at 18:27

1 Answers1

0

There's an easier way to do this, using a single timer which you use to update a display, and just keep track of whether you are running or walking - something like this

@IBAction func cmdStartStopAction(_ sender: Any)
{
    durationTimerDisplay = 0.1 // this will update the display every 0.1 seconds
    currentlyRunning = true // assume we start by running

    if timerDisplay == nil // this means we are starting the timer
    {
        durationRun = Double(txtTimerRun.text!)!    // assumes you have a text box to enter the times
        durationWalk = Double(txtTimerWalk.text!)!

        durationRemaining = durationRun             // we can use this to display a countdown timer

        // start the display update timer
        timerDisplay = Timer.scheduledTimer(timeInterval: durationTimerDisplay, target: self, selector: #selector(onTimerDisplay), userInfo: nil, repeats: true)
    }
    else
    {
        timerDisplay.invalidate()
        timerDisplay = nil
    }
}

@objc func onTimerDisplay()
{
    durationRemaining -= durationTimerDisplay       // count down timer
    if durationRemaining <= 0                       // switch between running and walking when you reach zero
    {
        // switch from running to walking, or walking to running
        currentlyRunning = !currentlyRunning
        if currentlyRunning {
            durationRemaining = durationRun
        }
        else {
            durationRemaining = durationWalk
        }
    }

    // create a display using a label lblTimer
    if currentlyRunning {
        lblTimer.text = String(format: "RUN: %.1f", durationRemaining)
    }
    else {
        lblTimer.text = String(format: "WALK: %.1f", durationRemaining)
    }
}
Russell
  • 5,436
  • 2
  • 19
  • 27