javascript - How can I emit values with delay in RXJS? - Stack Overflow

I have an array of values [1,2,3] .I want to emit each value withdelayI've managed to do it wi

I have an array of values [1,2,3] .

I want to emit each value with delay

I've managed to do it with the zip operator :

 Rx.Observable.from([1,2,3])
   .zip(Rx.Observable.timer(0, 1000), x => x)
   .subscribe((e) => console.log(e));

Question:

Is there any more appropriate operator for such task ? Involving an inner observable seems ( to me) incorrect approach.
Should I unsubscribe the inner Observable manually ? Becuase basically no one tells it to stop.

jsbin

I have an array of values [1,2,3] .

I want to emit each value with delay

I've managed to do it with the zip operator :

 Rx.Observable.from([1,2,3])
   .zip(Rx.Observable.timer(0, 1000), x => x)
   .subscribe((e) => console.log(e));

Question:

Is there any more appropriate operator for such task ? Involving an inner observable seems ( to me) incorrect approach.
Should I unsubscribe the inner Observable manually ? Becuase basically no one tells it to stop.

jsbin

Share Improve this question asked Dec 10, 2017 at 9:23 Royi NamirRoyi Namir 149k144 gold badges492 silver badges831 bronze badges 2
  • reactivex.io/documentation/operators/delay.html – user184994 Commented Dec 10, 2017 at 9:49
  • @user184994 that's not the same as what I wanted. I want to emit 1....delay...2 .....delay.....3. Not ....delay......1,2,3 – Royi Namir Commented Dec 10, 2017 at 9:49
Add a ment  | 

1 Answer 1

Reset to default 7

You can delay each emission itself and wait until the previous one pleted. Like this for example:

Rx.Observable.from([1,2,3])
   .concatMap(x => Observable.of(x).delay(1000)) // or Observable.timer(1000).mapTo(x)
   .subscribe((e) => console.log(e));

If you want to use zip you don't need to unsubscribe the timer but you need to tell it to plete (for example with take() or takeUntil()).

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744203732a4563003.html

相关推荐

  • javascript - How can I emit values with delay in RXJS? - Stack Overflow

    I have an array of values [1,2,3] .I want to emit each value withdelayI've managed to do it wi

    8天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信