Last active
November 2, 2016 14:22
-
-
Save mlabraca/52151fbbd5421b64fb6b to your computer and use it in GitHub Desktop.
Executes the block after a delay of x seconds.
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
//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