本文介绍了RxJS5随着时间的推移发出数组项并永远重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想随着时间的推移(每次发射之间间隔一秒)发射阵列项目,并且当所有项目都发射完后,一遍又一遍地重复.
I want to emit array items over time (a one second interval between each emit) and when all items have been emitted, repeat over and over.
我知道该怎么做,但是我想知道是否还有比..更简洁的东西.
I know how to do this, but I want to know if there is something more succinct than ..
const MY_ARRAY = ['one','two','three'];
const item$ = Rx.Observable.interval(1000).take(MY_ARRAY.length).repeat().map(x => MY_ARRAY[x]);
item$.subscribe(x => console.log(x));
谢谢
输出是..
两个"
三个"
一个"
两个"
三个"
等
ATOW,这里的答案总结为..
ATOW, the answers here are summarised as ..
const ARR = ['one', 'two', 'three'];
// TAKE YOUR PICK THEY ALL DO THE SAME
const item$ = Rx.Observable.interval(1000).map(i => ARR[i % ARR.length]);
// const item$ = Rx.Observable.interval(1000).zip(ARR, (a, x) => x).repeat();
// const item$ = Rx.Observable.interval(1000).zip(ARR).repeat().map(x => x[1]);
// const item$ = Rx.Observable.interval(1000).take(ARR.length).repeat().map(i => ARR[i]);
item$.subscribe((x) => {
console.log(x);
});
推荐答案
Observable.interval(1000).map(i => MY_ARRAY[i % MY_ARRAY.length])
这篇关于RxJS5随着时间的推移发出数组项并永远重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!