I want to ignore a response from my API if it is taking too long. I use
this.http.get(mysqlUrl).subscribe()
to get the response. But I want to cancel that subscription if it takes longer than 5 seconds to finish. I know I can use unsubscribe(), but how can I link that to a timeout value?
Thanks!
A plain setTimeout should work:
let subscription = this.http.get(mysqlUrl).subscribe(...);
setTimeout(() => subscription.unsubscribe(), 5000);
You could use the takeUntil operator:
this.http.get(mysqlUrl)
.takeUntil(Observable.of(true).delay(5000))
.subscribe(...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With