0

I want to set a Timer which acting like this: fire at 4 sec, fire at 15 sec, fire at 19 sec, then all repeat again, which means another 4 sec, 15 sec, 19 sec sequence again.

I found related topics to this:

Set a timer with variable intervals in swift

Swift Timers in Loop all fire at the same time instead of sequentially

but I still can't make them to fit my need.

Any suggestions appreciated.

Andrew
  • 1
  • 2
  • 2
    Fire a counter every second and count how many time you enter in it and do desired action at 4 sec, 15 sec, 19 sec. At 19 s you reset your counter. Use a property of the object that create the timer to keep track of the count. – Ptit Xav May 11 '22 at 16:04
  • Hi, thank you for the reply, I figure out how to do it: timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: {[weak self] timer in counter += 1 if counter == 4 { self?.soundCycleStart() } if counter == 15 { self?.soundCycleStart() } if counter == 19 { self?.soundCycleStart() } if counter > 19 { counter = 0 } }) it may looks stupid, but it works. Thanks – Andrew May 12 '22 at 13:38
  • I add no better or simplest idea. Good it can work. – Ptit Xav May 12 '22 at 14:25

0 Answers0