什么是相当于一个JavaScript的setInterval / setTimeout的在Android的/ Java的?setInterval、JavaScript、setTimeout、Java

2023-09-05 10:37:02 作者:萌蛋。

谁能告诉我,如果存在的android一个等价的setInterval / setTimeout的?没有任何人有关于如何做到这一点任何的例子吗?

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

在此先感谢!

推荐答案

像往常一样与Android有办法做到这一点的负载,但假设你只是想在同一个线程点点后运行一段codeA ,我用这样的:

As always with Android there's loads 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().postDelayed(
    new Runnable() {
        public void run() {
            Log.i("tag", "This'll run 300 milliseconds later");
        }
    }, 
300);

..这是pretty的多少等同于

.. this is pretty much equivalent to

setTimeout( 
    function() {
        console.log("This'll run 300 milliseconds later");
    },
300);