虚假和异步:jQuery的AJAX异步真实之间的区别是什么? [英] What is the difference between async:false and async:true in jquery ajax?

查看:159
本文介绍了虚假和异步:jQuery的AJAX异步真实之间的区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在jQuery的阿贾克斯应该有一个参数

In jquery ajax there should be a parameter

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

设定值之间的区别是什么真和假?

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

推荐答案

您将异步错误,当你需要一个Ajax请求完成,浏览器通过前其他codeS:

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的请求被设定为同步 。变量名是异步和值设置为真。这给了我一个小的混乱,以及时,首先了解它,所以让我们过目一下。

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.

同步(异步:假的) - 脚本停止并等待在继续之前发回的答复服务器。有些情况下,同步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)卸载事件处理任何事情,但如果你需要听到从服务器返回之前,你可以允许用户到导航离开页面,同步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) {
                // .....
         }
      });

异步(异步:真) - 当脚本允许页面继续进行处理,如果当它到达将处理答复。如果有什么不顺心的要求和/或文件的传输,程序仍然要认识到这个问题,并从中恢复的能力。 异步处理,同时避免了从服务器检索正在发生,因为你的访问者可以继续与网页互动,并请求的信息将与更新页面的响应进行处理,当它到达的延迟。

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) {
                    // .....
         }
       });

另外一起来看看这篇文章

Also take a look at this article

异步和同步AJAX调用

Asynchronous and Synchronous AJAX calls

这篇关于虚假和异步:jQuery的AJAX异步真实之间的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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