To delay a function call, use setTimeout() function. functionname − The function name for the function to be executed. milliseconds − The number of milliseconds. arg1, arg2, arg3 − These are the arguments passed to the function.
A setTimeout is a native JavaScript function, which calls a function or executes a code snippet after a specified delay (in milliseconds). A setTimeout accepts a reference to a function as the first argument. Alert messages will pop up after 5 seconds automatically.
performSelector:withObject:afterDelay:
Document Reference
You could also use a block
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[object method];
});
Most of time you will want to use dispatch_get_main_queue, although if there is no UI in the method you could use a global queue.
Edit:
Swift 3 version:
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
object.method()
}
Equally, DispatchQueue.global().asyncAfter(...
might also be a good option.
Best way to do is :
[self performSelector:@selector(YourFunctionName)
withObject:(can be Self or Object from other Classes)
afterDelay:(Time Of Delay)];
you can also pass nil as withObject parameter.
example :
[self performSelector:@selector(subscribe) withObject:self afterDelay:3.0 ];
There are already a lot of answers and they are all correct. In case you want to use the dispatch_after
you should be looking for the snippet which is included inside the Code Snippet Library
at the right bottom (where you can select the UI
elements).
So you just need to call this snippet by writing dispatch in code:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With