Skip to content

Instantly share code, notes, and snippets.

@jeksys
Last active February 13, 2017 03:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jeksys/cc9963f46bad83bfea9a to your computer and use it in GitHub Desktop.
Save jeksys/cc9963f46bad83bfea9a to your computer and use it in GitHub Desktop.
NStimer
extension NSTimer {
class func scheduledTimerWithTimeInterval(interval: NSTimeInterval, repeats: Bool, handler: NSTimer! -> Void) -> NSTimer {
let fireDate = interval + CFAbsoluteTimeGetCurrent()
let repeatInterval = repeats ? interval : 0
let timer = CFRunLoopTimerCreateWithHandler(kCFAllocatorDefault, fireDate, repeatInterval, 0, 0, handler)
CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, kCFRunLoopCommonModes)
return timer
}
}
// Usage:
var count = 0
NSTimer.scheduledTimerWithTimeInterval(1, repeats: true) { timer in
println(++count)
if count >= 10 {
timer.invalidate()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment