0

I am quite new to Swift and would love to know how to create a loop that runs some code every 30 seconds? I feel as though it is a basic question but I can't really find what I'm looking for.

0

3 Answers 3

0
func startTimer() {
    timer = Timer.scheduledTimer(timeInterval: 30, target: self, selector: #selector(UpdateTimer), userInfo: nil, repeats: true)
}

@objc func UpdateTimer() {
    counter += 1
}

to start

startTimer();

and to stop

timer.invalidate()
Sign up to request clarification or add additional context in comments.

Comments

-1

You can make Use of Timer

Timer.scheduledTimerWithTimeInterval(0.30, target: self, selector: #selector(timerFired), userInfo: userInfo, repeats: true)

timerFired: is the method that gets called after interval

@objc func timerFired() {
      print("Timer Called with interval 30seconds")
}

Comments

-1

You need to use scheduled timer. In viewDidLoad()

Timer.scheduledTimer(timeInterval: 30, target: self, selector: #selector(YourFuncName), userInfo: nil, repeats: true)

and func for selector

@objc func YourFuncName() {
      print("every 30seconds")
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.