I'm making an Android app with RxJava, in one of the page, I have a button, when pressed, the page will do a refresh. And I also want a auto-refresh for every 10 seconds, if user haven't pressed the button during that period. But when user clicks the button, I want the auto-refresh action to happen 10 seconds later after the click. Rather than continuing its own 10-second interval.
For example, at second 0, the app do a auto-refresh, then at second 3, user pressed the button. Then the auto-refresh should happen at second 13, second 23, etc.
I know that there is an interval()
operator that emits items at a certain interval. But it seems there is no way to "reset" the start time. Its kinda like unsubscribe and subscribe to the interval()
Observable again. A piece of code would be like
Observable<Long> intervalObservable = Observable.inteval(10, TimeUnit.SECONDS)
RxView.click(refreshButton).map(ignored -> 0L)
.merge(intervalObservable)
.subscibe(ignore -> performRefresh());
If there is a way to "unmerge" the intervalObservable
, then I can unmerge it in onNext
and then merge it again. But it seems there isn't.
How can I achieve this?