jquery ajax 中的 async:false 和 async:true 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20209097/
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
What is the difference between async:false and async:true in jquery ajax?
提问by Brutal_JL
In jquery ajax there is a parameter
在 jquery ajax 中有一个参数
$.ajax({async: true, ...});
What is the difference between setting the value to true
and false
?
将值设置为true
和之间有什么区别false
?
回答by Nidhish Krishnan
You set asyncto false, when you need that ajax request to be completed before the browser passes to other codes:
当您需要在浏览器传递给其他代码之前完成 ajax 请求时,您将async设置为false:
<script>
// ...
$.ajax(... async: false ...); // Hey browser! first complete this request,
// then go for other codes
$.ajax(...); // Executed after the completion of the previous async:false request.
</script>
By default, the
$.ajaxrequest in jQuery is set to asynchronous
. The variable name is async and the value is set to true. This gave me a little confusion as well when first learning about it, so let's go over it.
By default, the
$.ajaxrequest in jQuery is set to asynchronous
。变量名称为 async,值设置为 true。当我第一次了解它时,这也让我有点困惑,所以让我们回顾一下。
Synchronous ( async: false )– Script stops and waits for the server to send back a reply before continuing. There are some situations where Synchronous Ajax is mandatory.
同步 ( async: false )– 脚本停止并等待服务器在继续之前发回回复。在某些情况下,同步 Ajax 是必需的。
In standard Web applications, the interaction between the customer and the server is synchronous. This means that one has to happen after the other. If a customer clicks a link, the request is sent to the server, which then sends the results back.
在标准的 Web 应用程序中,客户和服务器之间的交互是同步的。这意味着必须一个接一个地发生。如果客户单击链接,请求将发送到服务器,然后服务器将结果发回。
Because of the danger of a request getting lost and hanging the browser, synchronous javascript isn't recommended for anything outside of (onbefore)unload event handlers, but if you need to hear back from the server before you can allow the user to navigate away from the page, synchronous Javascript isn't just your best option.
由于请求丢失并挂起浏览器的危险,不建议将同步 javascript 用于 (onbefore)unload 事件处理程序之外的任何内容,但如果您需要在允许用户导航之前收到服务器的回复从页面来看,同步 Javascript 不仅仅是您的最佳选择。
$.ajax({
url: "file.php",
type: "POST",
async: false,
success: function(data) {
// .....
}
});
Asynchronous ( async: true )– Where the script allows the page to continue to be processed and will handle the reply if and when it arrives. If anything goes wrong in the request and/or transfer of the file, your program still has the ability to recognize the problem and recover from it. Processing asynchronously avoids the delay while the retrieval from the server is taking place because your visitor can continue to interact with the web page and the requested information will be processed with the response updating the page as and when it arrives.
异步 ( async: true )– 脚本允许页面继续处理,并在响应到达时处理响应。如果文件的请求和/或传输出现任何问题,您的程序仍然能够识别问题并从中恢复。异步处理避免了从服务器检索时的延迟,因为您的访问者可以继续与网页交互,并且请求的信息将在响应到达时更新页面进行处理。
$.ajax({
url: "file.php",
type: "POST",
async: true,
success: function(data) {
// .....
}
});
Also take a look at this article
也看看这篇文章
回答by alex
Setting it to false
blocks the main thread (responsible for executing JavaScript, rendering the screen, etc) and waits for the XHR to complete.
将其设置为false
阻塞主线程(负责执行 JavaScript、渲染屏幕等)并等待 XHR 完成。
This is almost always a terrible idea. Users don't like unresponsive UIs.
这几乎总是一个可怕的想法。用户不喜欢无响应的 UI。
回答by Sabir Al Fateh
async(default: true
)
异步(默认值:true
)
Type: Boolean
类型:布尔型
By default, all requests are sent asynchronously (i.e. this is set to true
by default). If you need synchronous requests, set this option to false
. Cross-domain requests and dataType: "jsonp"
requests do not support synchronous operation. Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active. As of jQuery 1.8, the use of async: false
with jqXHR($.Deferred
) is deprecated; you must use the success/error/complete callback options instead of the corresponding methods of the jqXHR object such as jqXHR.done()
.
默认情况下,所有请求都是异步发送的(即true
默认设置为)。如果您需要同步请求,请将此选项设置为false
。跨域请求和dataType: "jsonp"
请求不支持同步操作。请注意,同步请求可能会暂时锁定浏览器,从而在请求处于活动状态时禁用任何操作。从 jQuery 1.8 开始,不推荐使用async: false
with jqXHR( $.Deferred
);您必须使用成功/错误/完成回调选项而不是 jqXHR 对象的相应方法,例如jqXHR.done()
.
Here is the Source.
这是来源。