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)
}
}