happyCustomers = 0;
followers = 0;
awardsWinning = 0;
photosTaken = 0;
arrayOfImages = [];
constructor() { }
ngOnInit() {
Observable.interval(400).subscribe(x => {
this.happyCustomers = this.happyCustomers + 1;
});
Observable.interval(200).subscribe(x => {
this.followers = this.followers + 1;
});
Observable.interval(700).subscribe(x => {
this.awardsWinning = this.awardsWinning + 1;
});
Observable.interval(300).subscribe(x => {
this.photosTaken = this.photosTaken + 1;
});
}
here i am adding + 1 evrytime and it does not stop, when i reach a certain data count it should stop the count.
答案 0 :(得分:3)
使用take方法。
take
方法从源获取第一个count
值,然后完成。在订阅前调用它。
const certainNumber = 10;
Observable.interval(400)
.take(certainNumber)
.subscribe(_ => this.happyCustomers++);
答案 1 :(得分:2)
If you have a hardcoded value, you can use the take() command.
Example :
Observable.interval(400)
.take(500)
.subscribe(x => {
this.happyCustomers = this.happyCustomers + 1;
});
The above code will stop after 500 events have been emitted.
See http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#instance-method-take