admin管理员组文章数量:1287919
I'm using this but interval operator wants to be on observable object its not exist on range, is there a way to have observable that emit for example emit 60 integer with interval of 1 second, i have been doing this
this.clock = Observable.range(1,60);
this.clock = this.clock.interval(1000).map(function(value){
console.log(value)
return value;
})
Its saying interval is not a function
also tried this:
this.clock = Observable.range(1,60).interval(1000).map(function(value){
console.log(value)
return value;
})
I'm using this but interval operator wants to be on observable object its not exist on range, is there a way to have observable that emit for example emit 60 integer with interval of 1 second, i have been doing this
this.clock = Observable.range(1,60);
this.clock = this.clock.interval(1000).map(function(value){
console.log(value)
return value;
})
Its saying interval is not a function
also tried this:
this.clock = Observable.range(1,60).interval(1000).map(function(value){
console.log(value)
return value;
})
Share
Improve this question
asked Nov 24, 2016 at 9:02
blackHawkblackHawk
6,30713 gold badges64 silver badges104 bronze badges
2
- 2 Do you want 60 integers from 0 to 59 during 60 seconds or do you want 60 integers during 60 seconds, with every integer as a random between some range ? – maxime1992 Commented Nov 24, 2016 at 9:50
- 1 No just sequential from 1 to 60 with time interval of 1 second – blackHawk Commented Nov 24, 2016 at 9:55
3 Answers
Reset to default 6To have a sequence from 1 to 60 with time interval of 1 second :
Observable
.interval(1000)
.map(x => x + 1) // to start from 1 instead of 0
.map(x => console.log(x)) // do some logic here
.take(60)
.subscribe();
The output here is :
1
2
3
.
.
.
58
59
60
Here's a snippet that you can run to see the output :
// just to have the working demo on SO
let output = '';
let divToUpdate = document.getElementById('counter');
// observable
Rx.Observable
.interval(1000)
.map(x => x + 1) // to start from 1 instead of 0
.map(x => {
output = `${output}<br>${x}`;
divToUpdate.innerHTML = output;
})
.take(60)
.subscribe();
<div id="counter"></div>
<script src="https://npmcdn./[email protected]/bundles/Rx.umd.js"></script>
Angular2 : PLUNKER DEMO
export class App {
name:string;
constructor() {
this.name = 'Angular2'
let rangeObs = Observable.zip(
Observable.range(0, 10),
Observable.interval(1000),
c => c);
rangeObs.subscribe(i => this.name = i;);
}
}
var clock = Observable
.interval(100)
.take(60)
.map(function(value){
console.log(value)
return value;
});
Use take(count).
版权声明:本文标题:javascript - How to emit integers from certain range with some interval of time using rxjs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741322203a2372268.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论