ajax AJAX同步和异步区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5528852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 10:59:15  来源:igfitidea点击:

AJAX sync and async difference

ajax

提问by randomwebdev

What's the difference between sync and async AJAX calls.When to use sync and when async?

同步和异步 AJAX 调用之间有什么区别。何时使用同步,何时使用异步?

回答by John Parker

At a very basic level, you use an asynchronous mode when you want the call to occur in the background and a synchronous mode when you want your code to wait until the call has completed.

在非常基本的层面上,当您希望调用在后台发生时使用异步模式,而当您希望代码等待调用完成时使用同步模式。

The asynchronous mode is the usual approach for AJAX calls, as you generally attach a callback function to the onreadystatechangeevent so that you can respond when the server-side data is ready, rather than waiting for the data to arrive.

异步模式是 AJAX 调用的常用方法,因为您通常将回调函数附加到onreadystatechange事件,以便您可以在服务器端数据准备好时做出响应,而不是等待数据到达。

回答by Phillip Schmidt

Async requests occur on a backgroundthread, meaning that the UI is not going to be blocked while the request is processing (there are a lot of exceptions to this when you get into states and I/O, etc.)

异步请求发生在后台线程上,这意味着在处理请求时 UI 不会被阻塞(当您进入状态和 I/O 等时,会有很多例外情况)

Lets say we have a 10 second web service call that needs to be made. If you call it synchronously, you're not going to be able to navigate to other pages, interact with the web page, etc. If you do it async, you will.

假设我们需要进行 10 秒的 Web 服务调用。如果您同步调用它,您将无法导航到其他页面、与网页交互等。如果您异步调用,则可以。