rx.Observable.throttleWithTimeout()方法的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(2.3k)|赞(0)|评价(0)|浏览(192)

本文整理了Java中rx.Observable.throttleWithTimeout()方法的一些代码示例,展示了Observable.throttleWithTimeout()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Observable.throttleWithTimeout()方法的具体详情如下:
包路径:rx.Observable
类名称:Observable
方法名:throttleWithTimeout

Observable.throttleWithTimeout介绍

[英]Returns an Observable that only emits those items emitted by the source Observable that are not followed by another emitted item within a specified time window.

Note: If the source Observable keeps emitting items more frequently than the length of the time window then no items will be emitted by the resulting Observable.

Information on debounce vs throttle:

代码示例

代码示例来源:origin: henrymorgen/android-advanced-light

private void throttleWithTimeOut() {
  Observable.create(new Observable.OnSubscribe<Integer>() {
    @Override
    public void call(Subscriber<? super Integer> subscriber) {
      for (int i = 0; i < 10; i++) {
        subscriber.onNext(i);
        int sleep = 100;
        if (i % 3 == 0) {
          sleep = 300;
        }
        try {
          Thread.sleep(sleep);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
      subscriber.onCompleted();
    }
}).throttleWithTimeout(200,TimeUnit.MILLISECONDS).subscribe(new Action1<Integer>() {
    @Override
    public void call(Integer integer) {
      Log.d(TAG, "throttleWithTimeOut:"+integer);
    }
  });
}

代码示例来源:origin: leeowenowen/rxjava-examples

@Override
 public void run() {
  Observable.create(new Observable.OnSubscribe<Integer>() {
   @Override
   public void call(Subscriber<? super Integer> subscriber) {
    for (int i = 0; i < 10; i++) {
     subscriber.onNext(i);
     sleep(500);
    }
    subscriber.onCompleted();
   }
  })
       .subscribeOn(Schedulers.newThread())
       .throttleWithTimeout(2, TimeUnit.SECONDS)
       .observeOn(Schedulers.newThread())
       .subscribe(new Action1<Integer>() {
        @Override
        public void call(Integer integer) {
         log(integer);
        }
       });
 }
});

相关文章

Observable类方法