在jquery ajax中,异步:false和异步:true有什么区别?

时间:2022-10-07 14:46:17

In jquery ajax there should be a parameter

在jquery ajax中应该有一个参数

$.ajax({async : true, ...});

What is the difference between setting the value to true and false?

将值设置为true和false的区别是什么?

2 个解决方案

#1


66  

You set async to 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.

默认情况下,美元。jQuery的ajaxrequest设置为异步。变量名为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)卸载事件处理程序,但是如果你需要听到从服务器之前,您可以允许用户导航离开页面,同步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)——脚本允许继续处理页面,并在响应到达时处理响应。如果在请求和/或文件传输中出现任何错误,您的程序仍然能够识别问题并从中恢复。异步处理避免了服务器检索时的延迟,因为访问者可以继续与web页面交互,请求的信息将在响应更新页面时进行处理。

$.ajax({
         url: "file.php",
         type: "POST",
         async: true,
         success: function(data) {
                    // .....
         }
       });

Also take a look at this article

还可以看看这篇文章

Asynchronous and Synchronous AJAX calls

异步和同步AJAX调用

#2


4  

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。

#1


66  

You set async to 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.

默认情况下,美元。jQuery的ajaxrequest设置为异步。变量名为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)卸载事件处理程序,但是如果你需要听到从服务器之前,您可以允许用户导航离开页面,同步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)——脚本允许继续处理页面,并在响应到达时处理响应。如果在请求和/或文件传输中出现任何错误,您的程序仍然能够识别问题并从中恢复。异步处理避免了服务器检索时的延迟,因为访问者可以继续与web页面交互,请求的信息将在响应更新页面时进行处理。

$.ajax({
         url: "file.php",
         type: "POST",
         async: true,
         success: function(data) {
                    // .....
         }
       });

Also take a look at this article

还可以看看这篇文章

Asynchronous and Synchronous AJAX calls

异步和同步AJAX调用

#2


4  

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。