我正在努力使用 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
, scan
和 takeWhile
如果您不想在开始时间依赖变量,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/