setinterval java code example

Example 1: java setinterval equivalent

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

Example 2: js setinterval

function func(){
  console.log("Ran")
}
setInterval(func,1000)//Runs the "func" function every second

Example 3: javascript setinterval

setInterval(function() {
  //Your code
}, 1000); //Every 1000ms = 1sec

Example 4: équivalent setTimeInterval java

// Param is optional, to run task on UI thread.     
Handler handler = new Handler(Looper.getMainLooper());
Runnable runnable = new Runnable() {
    @Override
    public void run() {
        // Do the task...
        handler.postDelayed(this, milliseconds) // Optional, to repeat the task.
    }
};
handler.postDelayed(runnable, milliseconds);

// Stop a repeating task like this.
handler.removeCallbacks(runnable);