Open acostaf opened 7 years ago
You can use either .delaySubscription
or a higher-order Observable
to do it.
// With Delay Subscription
ajaxRequest.delaySubscription(500).subscribe(processResult);
// With HOO
Observable.timer(500).flatMapLatest(ajaxRequest).subscribe(processResult);
Hi Paul, thanks for the quick answer.
Is ajaxRequest an observable ?
I am getting an error with delaySubscription saying that delaySubscription is not found ( https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/delaysubscription.md).
@acostaf yes it is a place holder for whatever your ajax call will be.
What version of RxJS are you using?
5.0.2
Ah ok. I need to start leading with that next time. You are in the wrong repo. V5 was moved to https://github.com/ReactiveX/rxjs.
AFAIK v5 does not have delaySubscription
so you would be left with the second approach I outlined, but it would be Observable.timer(500).switchMapTo(ajaxRequest).subscribe(processResult);
instead.
You can try posting in the other repo or SO as well if you don't feel that adequately answers your question.
Ok, will try it tomorrow, thanks for your help.
Thanks Paul that worked.
@paulpdaniels I need to stop the obeservable.timer in ngOnDestroy. It is not working for me. Can you please help me on this.
FYI.. Below is the code using in my project.
this.serverUrlSubscription = this.serverUrlStream
.debounce((val)=> Observable.timer(initial ? 0 : 500))
.filter(val => !!val)
.switchMap(val =>{
this.canLeave = false;
this.apiClientService.ocServiceUrl = val;
return Observable.fromPromise(this.apiClientService.getHospitals());
})
.catch((err, caught)=>{
console.log(err);
return caught;
}).subscribe((hospitals: Hospital[]) => {
this.hospitals = hospitals;
if (hospitals.length > 0) {
this.settings.hospitalInstallId = hospitals[0].InstallId;
this.canLeave = true;
this.cd.detectChanges();
}
});
So every 500ms this.apiClientService.getHospitals() http call is firing. This is fine but I need to stop this http call when I move to other page. So I tried in ngOnDestroy to kill that http call . Code: ngOnDestroy(){ this.serverUrlSubscription.unsubscribe(); }
When I move to next page this ngOnDestory is firing but at the same time http call is executing every 500ms. unsuscribe() is not working here. Please suggest.
Hi Guys
using delay just send the next item when the time is due but still makes the ajax call immediately.
How can I delay the call to the server side ?