Why don't synchronous ajax calls in jquery support timeout?

别等时光非礼了梦想. 提交于 2019-12-19 03:40:32

问题


I was reading here that synchronous ajax calls in jquery don't time out.

Is this a technical limitation, or just something the authors didn't feel like supporting? What actually happens under the table when an ajax call is synchronous?


回答1:


The browser handles synchronous/asynchronous requests via the XMLHttpRequest API, but it does not have a built-in timeout feature. JQuery implements its own timeout in JavaScript on top of the browser XHR API.

Since a synchronous call blocks all running scripts, the JQuery timeout does not work. In theory, it seems like synchronous timeouts would be possible if they were implemented at the browser level, as opposed to the JS level.




回答2:


A synchronous AJAX call blocks until the request has been finished. Implementing a timeout is not possible for technical reasons, because the AJAX call would have to be executed later.

If an AJAX call is executed later, the function somehow has to implement a blocking feature, to stop the code from running further after the AJAX call, and execute it again after a timeout - not possible.




回答3:


Indeed, is something related to the nature of AJAX request, jQuery cannot modify this.

I think timeout should be implemented in the server side in those cases.



来源:https://stackoverflow.com/questions/7919881/why-dont-synchronous-ajax-calls-in-jquery-support-timeout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!