Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I call function every 10 sec?

I want make an app that call a function for example every 10 sec.

I wrote my code like this:

Handler ha=new Handler();
ha.postDelayed(new Runnable() {
    @Override
    public void run() {
        //call function

    }
}, 10000); 

But my function call just one time in 10 sec after compile this code.

How can I fix it?

like image 244
user3257386 Avatar asked Feb 04 '14 13:02

user3257386


People also ask

How do you call a function after every second?

The setInterval() method calls a function at specified intervals (in milliseconds). The setInterval() method continues calling the function until clearInterval() is called, or the window is closed. 1 second = 1000 milliseconds.

How do you call a function every time?

Answer: Use the JavaScript setInterval() method You can use the JavaScript setInterval() method to execute a function repeatedly after a certain time period. The setInterval() method requires two parameters first one is typically a function or an expression and the other is time delay in milliseconds.

How do you call a function repeatedly every 5 seconds in JavaScript?

The setInterval() method in JavaScript can be used to perform periodic evaluation of expression or call a JavaScript function.

How do you call a function every 10 seconds TypeScript?

Use the setInterval() method to call a function every N seconds in TypeScript, e.g. setInterval(myFunction, seconds * 1000) . The first parameter the method takes is the function that will be called on a timer, and the second parameter is the delay in milliseconds.


3 Answers

Do it like this:

final Handler ha=new Handler();
ha.postDelayed(new Runnable() {

    @Override
    public void run() {
        //call function

        ha.postDelayed(this, 10000);
    }
}, 10000);
like image 187
Rudi Avatar answered Nov 09 '22 20:11

Rudi


Use a combination of Timer and TimerTask like this:

int delay = 0; // delay for 0 sec. 
int period = 10000; // repeat every 10 sec. 
Timer timer = new Timer(); 
timer.scheduleAtFixedRate(new TimerTask() 
{ 
    public void run() 
    { 
        //Call function
    } 
}, delay, period); 

Also make sure to use runOnUiThread() if you want to modify the UI.

like image 39
FD_ Avatar answered Nov 09 '22 20:11

FD_


You can use Rx java & Rx Android by adding this dependency as below :

//Rx Java
implementation 'io.reactivex.rxjava2:rxjava:2.2.13'

//Rx Android
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'

checkout Here for the latest version.

You need an Observable like this :

private final Observable etaUpdateRepeatableObservable =
        Observable
                .interval(ETA_UPDATE_INTERVALS, TimeUnit.MINUTES)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .repeat();

just change ETA_UPDATE_INTERVALS to your specific value.

You need a Disposable for subscribing to observable and to dispose it when required (Like onCleared() on ViewModels)

private Disposable etaUpdateDisposable;

You need a Consumer that your repeated logic would go there.

private final Consumer etaUpdateConsumer = o -> {
    //Here you got the repeated logic
};

Now you can subscribe(Start repeating function) and dispose(stop) observable every time you need.

  private void addEtaUpdateDisposable() {
    if (etaUpdateDisposable == null) {
        etaUpdateDisposable = etaUpdateRepeatableObservable.subscribe(etaUpdateConsumer);
    }
}

private void disposeEtaUpdate() {
    if (
            etaUpdateDisposable != null &&
                    !etaUpdateDisposable.isDisposed()
    ) {
        etaUpdateDisposable.dispose();
        etaUpdateDisposable = null;
    }
}
like image 6
Sepehr Avatar answered Nov 09 '22 19:11

Sepehr