我有2个计时器在20秒时设置一个,另一个在60时我需要在循环中重复这些6次,例如20然后60并重复但似乎无法这样做。每次我将它们放入循环中时,计时器都会加速。它应该调用timer1()然后调用timer2()并重复,但我不确定为什么不是。
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()
}