Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
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.
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()
Add a comment
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
timerFired
@objc func timerFired() { print("Timer Called with interval 30seconds") }
You need to use scheduled timer. In viewDidLoad()
viewDidLoad()
Timer.scheduledTimer(timeInterval: 30, target: self, selector: #selector(YourFuncName), userInfo: nil, repeats: true)
and func for selector
@objc func YourFuncName() { print("every 30seconds") }
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.