当前位置: 代码迷 >> 综合 >> Swift GCD-Timer
  详细解决方案

Swift GCD-Timer

热度:73   发布时间:2024-02-04 09:32:48.0
var seconds = 10
let timer = DispatchSource.makeTimerSource(flags: [], queue: DispatchQueue.global())
/*
dealine: 开始执行时间
repeating: 重复时间间隔
leeway: 时间精度
*/
timer.schedule(deadline: .now(), repeating: 1.0 , leeway: .nanoseconds(0))
timer.setEventHandler {seconds -= 1if seconds < 0 {timer.cancel()}else{print(seconds)}
}
timer.resume()