Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the equivalent to a JavaScript setInterval/setTimeout in Android/Java?

Can anyone tell me if an equivalent for setInterval/setTimeout exists for Android? Does anybody have any example about how to do it?

like image 258
karse23 Avatar asked Jan 27 '11 14:01

karse23


People also ask

Is there a setTimeout in Java?

In general, the setTimeout() method executes the function only once in contrast to the setInterval() method, and this method has another way that is to write this procedure with the window prefix or without it. Therefore in java, the setTimeout is a function provided in javascript and not in java.

What we can use instead of setTimeout in javascript?

The setInterval method has the same syntax as setTimeout : let timerId = setInterval(func|code, [delay], [arg1], [arg2], ...) All arguments have the same meaning. But unlike setTimeout it runs the function not only once, but regularly after the given interval of time.

What is difference between setTimeout and setInterval in javascript?

setTimeout(expression, timeout); runs the code/function once after the timeout. setInterval(expression, timeout); runs the code/function repeatedly, with the length of the timeout between each repeat.


1 Answers

As always with Android there's lots of ways to do this, but assuming you simply want to run a piece of code a little bit later on the same thread, I use this:

new android.os.Handler(Looper.getMainLooper()).postDelayed(     new Runnable() {         public void run() {             Log.i("tag", "This'll run 300 milliseconds later");         }     },  300); 

.. this is pretty much equivalent to

setTimeout(      function() {         console.log("This will run 300 milliseconds later");     }, 300); 
like image 156
Ben Clayton Avatar answered Sep 30 '22 09:09

Ben Clayton