Skip to content

Instantly share code, notes, and snippets.

@mlabraca
Last active November 2, 2016 14:22
Show Gist options
  • Save mlabraca/52151fbbd5421b64fb6b to your computer and use it in GitHub Desktop.
Save mlabraca/52151fbbd5421b64fb6b to your computer and use it in GitHub Desktop.
Executes the block after a delay of x seconds.
//Swfit 2
func delay(seconds seconds: Double, completionBlock: () -> ()) {
let delay = seconds * Double(NSEC_PER_SEC)
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
dispatch_after(time, dispatch_get_main_queue()) {
completionBlock()
}
}
//Swift 3
func delay(seconds: Double, completionBlock: @escaping () -> ()) {
DispatchQueue.main.asyncAfter(deadline: .now() + seconds) {
completionBlock()
}
}
delay(seconds: 2, completionBlock: { print("completed") })
delay(seconds: 2) {
print("completed")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment