-
-
Save wuxiaowei/96e7af0c828aed2470b3101e60bce80c to your computer and use it in GitHub Desktop.
NStimer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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