Get the App
SLTechnology News&Howtos  ›  Development  › 

How JavaScript sets timeout for XMLHttpRequests

Shulou Source: shulou.com Published: 2022-06-01 05:41:20 10月05日 Update

This article will explain in detail how JavaScript sets timeouts for XMLHttpRequests. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Sets the timeout for XMLHttpRequests.

After a XHR request takes a long time (for example, due to network problems), you may need to abort the request, so you can use setTimeout () with the XHR call.

Var xhr = new XMLHttpRequest (); xhr.onreadystatechange = function () {if (this.readyState = = 4) {clearTimeout (timeout); / / do something with response data}} var timeout = setTimeout (function () {xhr.abort (); / / call error callback}, 60,000 / * timeout after a minute * /); xhr.open ('GET', url, true); xhr.send ()

In addition, you should generally avoid synchronous Ajax requests altogether.

This is the end of this article on "how JavaScript sets timeouts for XMLHttpRequests". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Articles more good practical content articles knowledge network questions long-term reference synchronization help related Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Redmi MariaDB MySQL Linux