- SPM:
url: "github/eonist/SimpleTimer.git" .branch: "master"
- Manual: Copy paste:
SimplerTimer.swift
- Mac apps go into inactive state after 60 secs. Then this timer will slow down.
SimpleTimer(interval: 3, repeats: true){print("tick")}.start()//Ticks every 3 seconds
var idx: Int = 0
/**
* A Timer based on GCD (More reliable as it doesn't freeze if main thread is frozen)
* - Note: The can also be used: .seconds(Int), .microseconds(Int) and .nanoseconds(Int)
*/
func timerGCD() -> () {
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(100)) {
Swift.print("tick: idx\(self.idx)")
self.idx += 1
if self.idx < 10 {self.timerGCD()}//stop at 1sec
}
}