RxJS - 如何在不使用间隔的情况下逐步增加延迟时间? [英] RxJS - How to incrementally increase the delay time without using interval?

查看:54
本文介绍了RxJS - 如何在不使用间隔的情况下逐步增加延迟时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想逐渐增加延迟:

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));

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

I'm aware of using interval to incrementally increase the delay time as below. But I also need to consume this source 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) 开始时如何增量增加延迟时间;?

推荐答案

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

You can use the index of the emitted value and calculate the delay based on that.

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

这是一个包含您的代码段的完整示例的堆栈闪电战:https://stackblitz.com/edit/rxjs-jjmlta?devtoolsheight=60

Here is a stackblitz for a complete example with your snippet: https://stackblitz.com/edit/rxjs-jjmlta?devtoolsheight=60

这篇关于RxJS - 如何在不使用间隔的情况下逐步增加延迟时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆