【问题标题】:RxJava publishes only if there is no message for a whileRxJava 仅在一段时间内没有消息时发布
【发布时间】:2019-09-24 23:43:49
【问题描述】:

我正在尝试找到一种方法来在 RxJava 或 RxJs 中模拟以下代码。

var timerRef;

run()
run()

function run(){
   if(timerRef){
      clearTimeout(timerRef);
   }
   timerRef = setTimeout(function(){alert("hello")}, 1000);
}

基本上,一旦我收到发出的消息,我想等待一段时间并确保没有消息,如果有,取消当前消息并重复等待新消息,如果没有将它传递给订阅方法。

我知道,当前消息不应该知道上一个或下一个发送到管道的信息,但我该如何实现呢?

【问题讨论】:

    标签: rxjs rx-java reactive-programming reactive


    【解决方案1】:

    听起来像debounceTime 运算符是您的选择。这里我将 run() 替换为文档点击事件

    import { fromEvent } from 'rxjs';
    import { debounceTime, map } from 'rxjs/operators';
    
    
    const source = fromEvent(document, 'click')
    
    source.pipe(
      debounceTime(3000)
    )
    .subscribe(console.log);
    

    https://stackblitz.com/edit/typescript-fbyrsh

    【讨论】:

      猜你喜欢
      • 2011-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-10
      相关资源
      最近更新 更多