gpt4 book ai didi

rxjs - 如何使用 RxJS Observables 制作倒数计时器?

转载 作者:行者123 更新时间:2023-12-03 21:23:35 26 4
gpt4 key购买 nike

我正在努力使用 Observables 创建倒数计时器,示例位于 http://reactivex.io/documentation/operators/timer.html似乎不起作用。在此特定示例中,与 timerInterval 相关的错误不是从计时器返回的 Observable 的函数。

我也一直在尝试其他方法,我想出的最好的方法是:

Observable.interval(1000).take(10).subscribe(x => console.log(x));

这里的问题是它从 0 计数到 10,我想要一个倒数计时器,例如10,9,8...0。

我也试过这个,但是 timer Observable 类型不存在
Observable.range(10, 0).timer(1000).subscribe(x => console.log(x));

以及,它根本不产生任何输出。
Observable.range(10, 0).debounceTime(1000).subscribe(x => console.log(x));

To clarify I need help with ReactiveX's RxJS implementation, not the MircoSoft version.

最佳答案

使用 timer , scantakeWhile如果您不想在开始时间依赖变量,scan 中的第三个参数是起始编号

timer$ = timer(0, 1000).pipe(
scan(acc => --acc, 120),
takeWhile(x => x >= 0)
);

Check it out on Stackblitz

关于rxjs - 如何使用 RxJS Observables 制作倒数计时器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34921555/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com