How JavaScript sets timeout for XMLHttpRequests
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.