AJAX 请求同步与异步
同步请求和异步请求之间的性能(速度方面)有什么区别吗?
使用异步请求的原因是什么?
Is there any difference in performance ( speed wise ) between a synchronous request and and asynchronous request?
What all are the reasons for using an asynchronous request??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你应该总是使用异步 Ajax 请求,事实上我只知道应该使用同步 Ajax 请求的一个地方,那就是如果你在 Ajax 请求中在客户端嵌入一个新的 JavaScript 文件,然后引用类型和/或来自原始 Ajax 请求返回的 JavaScript 文件中的对象。 然后,应该(或者可以合理地)通过使用同步 Ajax 请求来获取这个新的 JS 文件...
除此之外,您应该始终使用异步 Ajax 请求。 最重要的原因是同步 Ajax 请求使 UI(浏览器)无响应......
You should mostly always use an asynchronous Ajax Request, in fact I know only about one place where a synchronous Ajax Request should be used which is if you're in an Ajax Request embedding a new JavaScript file on the client and then referencing types and/or objects from that JavaScript file in the return from the original Ajax Request. Then the fetching of this new JS file should (or can sanely) be included through using a synchronous Ajax Request...
Other then that you should always use asynchronous Ajax Requests. The most important reason is that a synchronous Ajax Request makes the UI (browser) unresponsive...
@Thomas Hansen 的答案是正确的,但我找到了一个明确的解释,其中包含异步的好处。
同步很简单,但是需要等待服务器响应,因此在处理请求时会阻塞调用者一段时间,并且比异步慢。
异步是必需设置,提交请求后不等待,立即将控制权返回给调用者,因此比同步更快。
当我必须使用上传控件时,我来到这里,该控件具有问题中的两种功能,并查看此功能的优缺点。
我得到了另一个链接,它用一个真实的例子进行了解释。 (链接是特定工具,所以要理解逻辑)
主要区别是我们服务器的响应时间。 上传时,同步将验证文件并实时创建密码。 异步会将文件发送到我们服务器的队列,并在处理完成后通过电子邮件发送通知。
同步对于多个大型 CSV 文件来说并不理想,因为您需要等到服务器处理该文件才能提交另一个请求。 对于大文件,这还可能导致您的浏览器由于服务器太忙而返回超时错误。 如果您有多个文件,异步将允许您将多个文件提交到服务器队列,以便在完成后通过电子邮件收据进行处理。
https: //www.aspsnippets.com/Articles/Difference- Between-Synchronous-Sync-and-Asynchronous-Async-Request-Call-in-AJAX.aspx
@Thomas Hansen answer is right but I found a clear explanation with benefits of
Asynchronous
.Synchronous is simple, but wait for the server response, thus block the execution of the caller for a period and slower than asynchronous while processing the request.
Asynchronous is required setting, do not wait after submitting the request and immediately return control to the caller, thus faster than Synchronous.
I am coming here when I have to use upload control which has both functionalities in the question and looking the pros and cons of this functionality.
I got the another link which explained with a real example. (the link is the specific tool, so understand the logic)
The major difference is the response time from our servers. At the time of upload, synchronous will validate file and create the passcode in real-time. Asynchronous will send the file to our server's queue and deliver notification via email once it is processed.
Synchronous is not ideal for multiple, large CSV file since you will need to wait until the file is processed by the server to submit another request. On large files, this also may cause your browsers to return with timeout errors due to the server being too busy. If you have multiple files, asynchronous will allow you to submit multiple files to the server queue to be processed with a email receipt once completed.
https://www.aspsnippets.com/Articles/Difference-between-Synchronous-Sync-and-Asynchronous-Async-Request-Call-in-AJAX.aspx
当涉及高度数据库密集型应用程序时,可能会导致严重的性能影响..尽管这种情况不太可能发生..并且发送许多同步ajax调用可能会造成积压..因此,如果应用程序数据库密集型并且一次发送如此多的请求最好将其设为异步。
当其设置为异步时,浏览器将使所有无响应的请求失败并继续新的一次。
there can be serious performance implications caused when it come to highly database intensive applications .. although it's very unlikely to happen .. and sending many Synchronous ajax calls can create a backlog .. so if the application database intensive and sending so many request at once it is better to make it asynchronous.
when its set to asynch the browser will fail all unresponsive request and continue with new once..