Reputation: 185
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
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