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

前端 未结 10 1150
遥遥无期
遥遥无期 2020-11-29 16:54

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

10条回答
  •  独厮守ぢ
    2020-11-29 17:56

    setInterval()

    function that repeats itself in every n milliseconds

    Javascript

     setInterval(function(){ Console.log("A Kiss every 5 seconds"); }, 5000);
    

    Approximate java Equivalent

    new Timer().scheduleAtFixedRate(new TimerTask(){
        @Override
        public void run(){
           Log.i("tag", "A Kiss every 5 seconds");
        }
    },0,5000);
    

    setTimeout()

    function that works only after n milliseconds

    Javascript

    setTimeout(function(){ Console.log("A Kiss after 5 seconds"); },5000);
    

    Approximate java Equivalent

    new android.os.Handler().postDelayed(
        new Runnable() {
            public void run() {
                Log.i("tag","A Kiss after 5 seconds");
            }
    }, 5000);
    

提交回复
热议问题