Skip to content

Instantly share code, notes, and snippets.

@brocoo
Last active September 27, 2019 13:49
Show Gist options
  • Save brocoo/290d53dd3404b9daed7c to your computer and use it in GitHub Desktop.
Save brocoo/290d53dd3404b9daed7c to your computer and use it in GitHub Desktop.
Delay the execution of a given closure on the main queue
public func delay(time: Double, completion: () -> Void) {
let ms = dispatch_time(DISPATCH_TIME_NOW, Int64(time * Double(NSEC_PER_SEC)))
dispatch_after(ms, dispatch_get_main_queue()) {
() -> Void in
completion()
}
}
public func delay(time: Double) -> (()->Void) -> Void {
return {
(closure) -> Void in
let ms = dispatch_time(DISPATCH_TIME_NOW, Int64(time * Double(NSEC_PER_SEC)))
dispatch_after(ms, dispatch_get_main_queue()) {
() -> Void in
closure()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment