Can anyone tell me if an equivalent for setInterval/setTimeout exists for Android? Does anybody have any example about how to do it?
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.
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.
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.
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);
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