Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java equivalent of JavaScript Timeout functions

I am translating some Javascript code into Java, and need to know how to do Timeouts in Java. How would I translate the following into Java?

var x = setTimeout(function(){ ... }, 2000);
clearTimeout(x);
like image 651
Billy Moon Avatar asked Jun 18 '26 22:06

Billy Moon


1 Answers

Try out with java.util.Timer

You can schedule your task when the specified time is over or into a periodic interval. Use

myTimer.schedule(tt, 2000);

where tt is a TimerTask object from java.util.TimerTask (you have to implement the run() method, who will correspond to your "function(){ ... }" in javascript).

like image 192
cabreracanal Avatar answered Jun 20 '26 12:06

cabreracanal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!