# Create while loop on timer (swift3)

**URL:** <https://forums.kodeco.com/t/create-while-loop-on-timer-swift3/31045>\
**Category:** Swift\
**Created:** [June 17, 2017, 2:56pm UTC](https://forums.kodeco.com/t/create-while-loop-on-timer-swift3/31045 "2017-06-17T14:56:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![zalubski](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/zalubski/32/81494_2.png) [@zalubski](https://forums.kodeco.com/u/zalubski)\
**Post date:** [June 17, 2017, 2:56pm UTC](https://forums.kodeco.com/t/create-while-loop-on-timer-swift3/31045/1 "2017-06-17T14:56:20Z")

</div>

The code below when compiled is completed instantly. I would like the counter to add 1 every second. So the program should run for 556 seconds. Every second goes by the counter increase by 1.

```
                   var counter = 0

    while true {

        counter += 1

        print(counter)
        
        if counter == 556 {
            break
        }
    }

```

---

<div class="post-metadata">

**Author:** ![narrativium](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/narrativium/32/110937_2.png) [@narrativium](https://forums.kodeco.com/u/narrativium)\
**Post date:** [June 17, 2017, 9:42pm UTC](https://forums.kodeco.com/t/create-while-loop-on-timer-swift3/31045/2 "2017-06-17T21:42:14Z")

</div>

Use a `Timer`. Give it an interval of 1 second, set repeat to true, and give it a function to call every time it fires. The 557th time it fires, you can tell the timer to `invalidate` and it’ll stop firing.
