# Run loop based intervals playing sound swift4

**URL:** <https://forums.kodeco.com/t/run-loop-based-intervals-playing-sound-swift4/41921>\
**Category:** Swift\
**Created:** [April 25, 2018, 1:31am UTC](https://forums.kodeco.com/t/run-loop-based-intervals-playing-sound-swift4/41921 "2018-04-25T01:31:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![timswift](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/timswift/32/110937_2.png) [@timswift](https://forums.kodeco.com/u/timswift)\
**Post date:** [April 25, 2018, 1:31am UTC](https://forums.kodeco.com/t/run-loop-based-intervals-playing-sound-swift4/41921/1 "2018-04-25T01:31:39Z")

</div>

I want to make a app that plays a sound based on intervals that the user enters. So say if the user enters 2 times per 10 seconds. The sound should play at 5 seconds and 10 seconds.

```
               import UIKit
class ViewController: UIViewController {
@IBOutlet var playbutton: UIButton!
@IBOutlet var titlelabel: UILabel!
@IBOutlet var judo: UITextField!
 @IBOutlet var judo2: UITextField!
 var timer : Timer?
 var counter = 0.0
var isRunning = false
override func viewDidLoad() {
super.viewDidLoad()
titlelabel.text = "\(counter)"
playbutton.isEnabled = true
}
 @IBAction func btnplay(_ sender: UIButton) {
if timer == nil {
    timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(UpdateTime), userInfo: nil, repeats: true)
    playbutton.isEnabled = false
  }
}
@objc func UpdateTime(){
counter += 0.1
titlelabel.text = String(format: "%.1f", counter)
let numberOfTimes = Double(judo.text ?? "") ?? 0.0
 let maxTime = Double(judo2.text ?? "") ?? 0.0
if counter >= maxTime {

    timer?.invalidate()
    timer = nil

    }
  if counter <= maxTime {
    ////RUN LOOP
}
}
}

```

---

<div class="post-metadata">

**Author:** ![jayantvarma](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jayantvarma/32/111768_2.png) [@jayantvarma](https://forums.kodeco.com/u/jayantvarma)\
**Post date:** [April 25, 2018, 5:03am UTC](https://forums.kodeco.com/t/run-loop-based-intervals-playing-sound-swift4/41921/2 "2018-04-25T05:03:06Z")

</div>

Hi @timswift,  
hope your projects are progressing, good to see you with a newer issue.

You have the right idea on how to solve this issue. Is there a specific reason why you want to count by yourself in each timer event?

There are a couple of things you must bear in mind when trying to use timers,

1. Timers are not High-resolution
2. Timers do not return till the code in the event handler is executed completely

with that in mind, you are better off getting an event at the time interval you want than trying to calculate the ticks yourself.

cheers,

Jayant

---

<div class="post-metadata">

**Author:** ![timswift](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/timswift/32/110937_2.png) [@timswift](https://forums.kodeco.com/u/timswift)\
**Post date:** [April 25, 2018, 5:31pm UTC](https://forums.kodeco.com/t/run-loop-based-intervals-playing-sound-swift4/41921/3 "2018-04-25T17:31:14Z")

</div>

What do you mean by high resolution.

---

<div class="post-metadata">

**Author:** ![jayantvarma](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jayantvarma/32/111768_2.png) [@jayantvarma](https://forums.kodeco.com/u/jayantvarma)\
**Post date:** [April 26, 2018, 7:50am UTC](https://forums.kodeco.com/t/run-loop-based-intervals-playing-sound-swift4/41921/4 "2018-04-26T07:50:50Z")

</div>

Hi @timswift,  
TLDR; timers that offer you finer granularity and accuracy are called High-resolution timers. Here’s more reading in case you want [Technical Note TN2169: High Precision Timers in iOS / OS X](https://developer.apple.com/library/content/technotes/tn2169/_index.html)

cheers,

Jayant

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [October 8, 2018, 5:44pm UTC](https://forums.kodeco.com/t/run-loop-based-intervals-playing-sound-swift4/41921/5 "2018-10-08T17:44:18Z")

</div>

This topic was automatically closed after 166 days. New replies are no longer allowed.
