Skip to content
Advertisement

Detect if server response is slow and show alert on Javascript Fetch

I would like to know if there is a way that if my request takes more than 20 seconds, I can show an alert alerting users of an error in the API. So they can refresh the page or come back later.

Something like response.status.

I do NOT need setTimeout and AbortController. Just detect response and trigger the alert.

JavaScript

Advertisement

Answer

You need setTimeout to set the timer to 20 sec, and after the fetch request, if the request takes less than 20 seconds, clear the timeout

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement