Search code examples
httpangularionic-frameworkionic2observable

How to wait only for certain time to get response of HTTP request (GET/POST) in angular 2?


How to wait only for certain time to get the response of HTTP request (GET/POST) in angular 2 otherwise break the HTTP request?

I had a look on timeout and setTimeInterval functions but those do not break the HTTP request within specified time limit.

Basically, what I am trying to do is break the HTTP request (whether get or post) if it doesn't get the response within 5 seconds.


Solution

  • You can use rxjs timeout() and pass time in miliseconds.

    this.http.get('url').timeout(5000).map(data => data.json());