How can I delay a method call for 1 second?
performSelector:withObject:afterDelay:
Document Reference
How do I make a delay in Java?
If you want to pause then use java.util.concurrent.TimeUnit
:
TimeUnit.SECONDS.sleep(1);
To sleep for one second or
TimeUnit.MINUTES.sleep(1);
To sleep for a minute.
As this is a loop, this presents an inherent problem - drift. Every time you run code and then sleep you will be drifting a little bit from running, say, every second. If this is an issue then don't use sleep
.
Further, sleep
isn't very flexible when it comes to control.
For running a task every second or at a one second delay I would strongly recommend a ScheduledExecutorService
and either scheduleAtFixedRate
or scheduleWithFixedDelay
.
For example, to run the method myTask
every second (Java 8):
public static void main(String[] args) {
final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
executorService.scheduleAtFixedRate(App::myTask, 0, 1, TimeUnit.SECONDS);
}
private static void myTask() {
System.out.println("Running");
}
And in Java 7:
public static void main(String[] args) {
final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
executorService.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
myTask();
}
}, 0, 1, TimeUnit.SECONDS);
}
private static void myTask() {
System.out.println("Running");
}
Delay first function call for n-seconds
I guess you are not waiting until timeout to declare the interval function.
There is a few different ways to do that, so let me suggest one:
function ping() {
// code ...
}
setTimeout(function () {
ping(); // call function when page loads for first time
// call function every 30 seconds
window.setInterval(function(){
ping();
}, 30000);
}, 5000);
Delay a method call in objective-c
Once the method is executing then there is no way of stopping it.
But you can cancel if it is not fired. You can do something like this
//.... your code
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(myMethod) object:nil];
[self performSelector:@selector(myMethod) withObject:nil afterDelay:3.0];
//.... your code
In this way you can cancel previous perform request only if the myMethod
is not being fired.
Flutter - How to delay a function for some seconds
You'll have to add an helper variable in the outer scope, that will indicate whether the user is on an answer cooldown or not.
The shortest solution will be:
var answerCooldownInProgress = false;
tappedbutton(int index) async {
// Ignore user taps when cooldown is ongoing
if (answerCooldownInProgress) {
return;
}
final userAnswer = await userAnswer();
if (userAnswer) {
// ...
} else {
ErrorSnackbar();
answerCooldownInProgress = true;
await Future.delayed(const Duration(seconds: 2));
answerCooldownInProgress = false;
}
}
How to call a method after a delay in Android
Kotlin
Handler(Looper.getMainLooper()).postDelayed({
//Do something after 100ms
}, 100)
Java
final Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(new Runnable() {
@Override
public void run() {
//Do something after 100ms
}
}, 100);
How to delay a method call after 1 min in angular or rxjs using Delay operator
If you are using rxjs and Angular then you probably want to use the RxJs timer
subject
import { timer } from 'rxjs';
const minute = 1000 * 60;
function helloWorld() {
console.log('hello');
}
timer(minute).subscribe(helloWorld);
If you are looking into what RxJs Subject or Operator you want, try checking the RxJs Operator Decision Tree for help.
jQuery: Wait/Delay 1 second without executing code
$.delay is used to delay animations in a queue, not halt execution.
Instead of using a while loop, you need to recursively call a method that performs the check every second using setTimeout
:
var check = function(){
if(condition){
// run when condition is met
}
else {
setTimeout(check, 1000); // check again in a second
}
}
check();
Related Topics
Swift - Using Replacerange() to Change Certain Occurrences in a String
Writing an iOS 8 Share Extension Without a Storyboard
How to Get Navigation Based Template Functionality in Swift Programming
Cocoa Touch: How to Change Uiview's Border Color and Thickness
Xcode 8/Swift 3: "Expression of Type Uiviewcontroller? Is Unused" Warning
Add Shadow on Uiview Using Swift 3
Wait for Asynchronous Operation to Complete in Swift
Cfbundleversion Must Be Higher Than Previous Version
Appdelegate and Scenedelegate When Supporting iOS 12 and 13
Reverse an Audio File Swift/Objective-C
Html5 Video Autoplay on Iphone
Pass Parameter in Webservice in Swift
Custom Cell Row Height Setting in Storyboard Is Not Responding
Getting the Value of a Uitextfield as Keystrokes Are Entered
How to Hide Tab Bar with Animation in iOS