Home Trying to use a for loop to cycle through repeating timers in swift
Reply: 0

Trying to use a for loop to cycle through repeating timers in swift

user854
1#
user854 Published in May 22, 2018, 11:10 pm

I have 2 timers setup one at 20 seconds the other at 60 I need to repeat these in a loop 6 times, such as 20 then 60 and repeat but cannot seem to do so. Every time I place them in the loop the timer accelerates. It should call timer1() then timer2() and repeat but I'm not sure why its not.

 for _ in 1...6 {
        timer1()
        let when = DispatchTime.now() + 19 
        DispatchQueue.main.asyncAfter(deadline: when) {
            self.timer2()
            }
        }

func timer1() {
    beginnerStart()
    beginnerTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(ViewController.beginnerStart), userInfo: nil, repeats: true)

 @objc func beginnerStart() {
    warmUpCountDown = warmUpCountDown - 1
    if warmUpCountDown == 0 {
        beginnerTimer.invalidate()
    }
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.301625 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO