jquery ajax 中的 async:false 和 async:true 有什么区别? [英] What is the difference between async:false and async:true in jquery ajax?

查看:25
本文介绍了jquery ajax 中的 async:false 和 async:true 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在jquery ajax中有一个参数

In jquery ajax there is a parameter

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

将值设置为 truefalse 有什么区别?

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

推荐答案

当您需要在浏览器通过之前完成 ajax 请求时,您将 async 设置为 false到其他代码:

You set async to false, when you need that ajax request to be completed before the browser passes to other codes:

<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>

默认情况下,jQuery 中的$.ajax请求设置为异步.变量名称为 async,值设置为 true.刚开始学习的时候,这也让我有点困惑,所以让我们回顾一下.

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.

Synchronous ( async: false ) - 脚本停止并等待服务器发回回复,然后再继续.在某些情况下,同步 Ajax 是必需的.

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.

在标准的 Web 应用程序中,客户和服务器之间的交互是同步的.这意味着必须一个接一个地发生.如果客户点击链接,请求将发送到服务器,然后服务器将结果发回.

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.

由于请求丢失并挂起浏览器的危险,不建议将同步 javascript 用于 (onbefore)unload 事件处理程序之外的任何内容,但如果您需要在允许用户之前收到服务器的回复要离开页面,同步 Javascript 不仅仅是您的最佳选择.

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.

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

异步 ( async: true ) - 脚本允许页面继续被处理,并在它到达时处理回复.如果文件的请求和/或传输出现任何问题,您的程序仍然能够识别问题并从中恢复.异步处理避免了从服务器检索时的延迟,因为您的访问者可以继续与网页交互,并且请求的信息将在响应到达时更新页面进行处理.

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.

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

也看看这篇文章

异步和同步 AJAX 调用

这篇关于jquery ajax 中的 async:false 和 async:true 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆