Skip to content

Instantly share code, notes, and snippets.

@wuxiaowei
Forked from jeksys/NSTimer+closure.swift
Created January 9, 2017 02:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wuxiaowei/96e7af0c828aed2470b3101e60bce80c to your computer and use it in GitHub Desktop.
Save wuxiaowei/96e7af0c828aed2470b3101e60bce80c 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