Skip to content

Instantly share code, notes, and snippets.

@didats
Last active September 16, 2016 04:11
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 didats/a662cd5ab41d064f82b2 to your computer and use it in GitHub Desktop.
Save didats/a662cd5ab41d064f82b2 to your computer and use it in GitHub Desktop.
Delay execution in iOS (Objective C & Swift 2 & Swift 3)
func delay(withTime: Double, callback: @escaping () -> Void) {
let when = DispatchTime.now() + withTime
DispatchQueue.main.asyncAfter(deadline: when) {
callback()
}
}
double delayInSeconds = 0.5; // set the time
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
// do something here
});
let delayInSeconds = 4.5 * Double(NSEC_PER_SEC)
let popTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delayInSeconds))
dispatch_after(popTime, dispatch_get_main_queue()) {
// do something here
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment