【发布时间】:2020-07-27 12:18:50
【问题描述】:
我想限制使用“from”运算符创建的数组 observable 的内容,以显示每个值相隔 1 秒。按预期省略油门显示 1 到 10。如果我在可观察对象的 pipe() 中引入节流阀,则流仅输出“1”。
https://stackblitz.com/edit/rxjs-behaviorsubject-simpleexample-knappa?file=index.ts
// RxJS v6+
import { from, interval } from 'rxjs';
import { throttle } from 'rxjs/operators';
const observable = from([1,2,3,4,5,6,7,8,9,10]);
const example = observable.pipe(
// removing this line displays the full array all at once
throttle(ev => interval(1000))
// what we want is 1..2..3..4.. printed in succession, 1 number a second
);
example.subscribe(x => console.log(x));
如何打印相隔 1 秒的数组的数字?
通过扩展,我如何将数字添加到使用“from”创建的流中并继续间隔 1 秒限制这些值?
这很不直观。任何帮助将不胜感激。提前致谢。
【问题讨论】:
-
你不需要油门,这在 RxJS 中有点不同。查看此答案以获取更多信息:stackoverflow.com/a/57097217/8578281
标签: arrays rxjs throttling