RxJS 5, Angular: How to do a request every x seconds UNTIL a message matches a certain string?


#1

New to Web-dev and Angular. Here is my current code. Right now, all it does is calling a http requests every 2seconds and console log the message. What I want is if the message matches to a string “5”, let it unsubscribe. Also, how do i make this as non-nested subscribe? (is nested subcribe a bad practice?). Currently, i’m using Angular 5 and rxjs 5.

public checkProgress() {
   Observable
      .interval(2000)
      .subscribe(
        x => {
          this.myService.getStatus()
            .subscribe( data => console.log(data));
        }
      );
  }

Also, if a user navigate to a different component, how do I unsubscribe it? ngOnDestroy?


#2

@teodor or @Supun may be able to help with this. If not, I suggest posting this question on StackOverflow.


#3

I don’t know how to get the message from Browser Console.

If I were you, I would create a protected variable in component class and increment it; when it reaches 5, perform the operation. Here’s a demo:

export class Component
{
    protected incrementedVar : int;
    
    public constructor()
    { this.incrementedVar = 0;}

    public incrementVar() : void
    {this.incrementedVar++;}

    public checkIt() // : mixed
    { if(this.incrementedVar === 5) do stuff }

}

#4

This question should probably be asked on stackoverflow.com tagging your programming language. :slight_smile: