Solution to stop rotating numbers in angular 2

 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 add + 1 evrytime and it doesn't stop when i reach a certain amount of data it should stop counting.

+3


source to share


2 answers


Use take .

take

the method takes the first count

values ​​from the source and then exits.

Call it before subscribing.



const certainNumber = 10;

Observable.interval(400)
  .take(certainNumber)
  .subscribe(_ => this.happyCustomers++);

      

+3


source


If you have a hard 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 fired.

See http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#instance-method-take

+2


source







All Articles