简体   繁体   English

RxJS Interval没有延迟

[英]RxJS Interval without delay

The following code emits an int after 5000ms and then another in every 5000ms later: 以下代码在5000ms后发出一个int ,然后在每5000ms后发出另一个:

let evens = Observable.interval(5000)
     .map(i => {
          return i * 2;
      });

 evens.subscribe((i) => {
      console.log(i);
 });

Is it possible to do this, but have the first result immediately ( 0ms ), and then wait 5000m s between subsequent results? 是否可以这样做,但是立即得到第一个结果( 0ms ),然后在后续结果之间等待5000m

You can use timer() where the first parameter is the initial delay: 您可以使用timer() ,其中第一个参数是初始延迟:

timer(0, 5000);

Or if you want the first item to be emitted immediately you can also use the startWith() operator. 或者,如果您希望立即发出第一个项目,也可以使用startWith()运算符。

Jan 2019: Updated for RxJS 6 2019年1月:更新了RxJS 6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM