【问题标题】:RxJS - How to incrementally increase the delay time without using interval?RxJS - 如何在不使用间隔的情况下逐步增加延迟时间?
【发布时间】:2021-02-13 22:19:29
【问题描述】:

我想为此逐步增加延迟:

const source = from(839283, 1123123, 63527, 4412454); // note: this is random
const spread = source.pipe(concatMap(value => of(value).pipe(delay(1000)))); // how to incrementally increase the delay where the first item emits in a second, the second item emits in three seconds, the third item emits in five seconds, and the last item emits in seven seconds.
spread.subscribe(value => console.log(value));

我知道使用间隔来逐步增加延迟时间,如下所示。但我也需要消费这个来源const source = from(839283, 1123123, 63527, 4412454);

const source = interval(1000); // But I also need to use have this from(839283, 1123123, 63527, 4412454)
const spread = source.pipe(concatMap(value => of(value).pipe(delay(value * 200))));
spread.subscribe(value => console.log(value

const source = from(839283, 1123123, 63527, 4412454); 开头时如何递增延迟时间?

【问题讨论】:

    标签: angular rxjs rxjs5 rxjs6 rxjs-observables


    【解决方案1】:

    您可以使用发出值的索引并据此计算延迟。

    concatMap((value, index) => {
      return of(value).pipe(delay((index > 2 ? 7 : index) * 1000));
    })
    

    这是一个堆栈闪电战,其中包含您的 sn-p 的完整示例: https://stackblitz.com/edit/rxjs-jjmlta?devtoolsheight=60

    【讨论】:

      猜你喜欢
      • 2017-07-27
      • 2021-10-27
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 2021-12-21
      • 1970-01-01
      • 1970-01-01
      • 2012-05-09
      相关资源
      最近更新 更多