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;
});
}
在这里,我添加+ 1 evrytime并且它不会停止,当我达到某个数据计数时,它应该停止计数。
使用 take 方法。
take
方法从源中获取前count
个值,然后完成。在订阅前调用它。
const certainNumber = 10;
Observable.interval(400)
.take(certainNumber)
.subscribe(_ => this.happyCustomers++);
如果你有一个硬编码的值,你可以使用 take(( 命令。
例:
Observable.interval(400)
.take(500)
.subscribe(x => {
this.happyCustomers = this.happyCustomers + 1;
});
上面的代码将在发出 500 个事件后停止。
见 http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#instance-method-take