何时在 jquery 的 ajax 函数中使用 async false 和 async true

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18366118/
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-08-26 21:26:46  来源:igfitidea点击:

When to use async false and async true in ajax function in jquery

ajaxjquery

提问by user2569524

When to use use async false or async true in an ajax call. In terms of performance does it make any difference ?

何时在 ajax 调用中使用 async false 或 async true 。在性能方面它有什么区别吗?

example :

例子 :

$.ajax({
        url : endpoint,
        type : "post",
        async : false,
        success : function(data) {
                if (i==1){  
                getMetricData(data)}

                else if (i==2)
                {
                    capture = data;
                }

        }
    });

回答by Amin Saqi

It's not relative to performance...

这与性能无关...

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

It is best practice to go asynchronous if you can do several things in parallel (no inter-dependencies). If you need it to complete to continue loading the next thing you could use synchronous, but note that this option is deprecated to avoid abuse of sync:

如果您可以并行执行多项操作(没有相互依赖关系),则最好采用异步方式。如果您需要它完成以继续加载下一件事,您可以使用同步,但请注意,不推荐使用此选项以避免滥用同步:

jQuery.ajax() method's async option deprecated, what now?

不推荐使用 jQuery.ajax() 方法的异步选项,现在怎么办?

回答by Adarsh Babu PR

ShowPopUpForToDoList: function (id, apprId, tab) {
    var snapShot = "isFromAlert";
    if (tab != "Request")
        snapShot = "isFromTodoList";
    $.ajax({
        type: "GET",
        url: common.GetRootUrl('ActionForm/SetParamForToDoList'),
        data: { id: id, tab: tab },
        async:false,
        success: function (data) {
            ActionForm.EditActionFormPopup(id, snapShot);
        }
    });
},

Here SetParamForToDoListwill be excecuted first after the function ActionForm.EditActionFormPopupwill fire.

这里SetParamForToDoList将在函数ActionForm.EditActionFormPopup触发后首先执行。

回答by Chaudhary

  1. When async setting is set to false, a Synchronous call is made instead of an Asynchronous call.
  2. When the async setting of the jQuery AJAX function is set to true then a jQuery Asynchronous call is made. AJAX itself means Asynchronous JavaScript and XML and hence if you make it Synchronous by setting async setting to false, it will no longer be an AJAX call.
  3. for more information please refer this link
  1. 当 async 设置设置为 false 时,将进行同步调用而不是异步调用。
  2. 当 jQuery AJAX 函数的异步设置设置为 true 时,就会进行 jQuery 异步调用。AJAX 本身意味着异步 JavaScript 和 XML,因此如果您通过将 async 设置设置为 false 使其成为同步,它将不再是 AJAX 调用。
  3. 有关更多信息,请参阅此链接