FanaticTyp
FanaticTyp

Reputation: 185

How to stop a IntervalObservable

The following code, sends every 1 sec a http request. This works fine, but I didn't find a way, how I can close/finish the webservice request programmatically.

// Service Class

public getData() {

     return IntervalObservable.create(1000)
     .flatMap(() => {
         return this.http.post(this.uri)
         .map(res => res.text());
     });
    }

// Main Class

 testInterval ()
    {

      var obs = this.servletService.getData()

      obs.subscribe(

      (data) => {


      },
      (error) => {
      });
    }

Upvotes: 2

Views: 628

Answers (1)

kimy82
kimy82

Reputation: 4465

To stop the interval save the subscriber to a variable and unsubscribe it when necessary.

 this.subscriber = obs.subscribe( ... );

 ...

 if(this.subscriber.isStopped) {
  this.subscriber.unsubscribe();
 }

Upvotes: 2

Related Questions