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

时间:2022-10-07 14:55:51

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

        }
    });

4 个解决方案

#1


26  

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:

您将异步设置为false,当您需要在浏览器传递到其他代码之前完成ajax请求:

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

#2


4  

  1. When async setting is set to false, a Synchronous call is made instead of an Asynchronous call.
  2. 当异步设置为false时,将发出同步调用而不是异步调用。
  3. 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.
  4. 当jQuery AJAX函数的异步设置为true时,将进行jQuery异步调用。AJAX本身意味着异步JavaScript和XML,因此如果通过将异步设置为false使其同步,它将不再是AJAX调用。
  5. for more information please refer this link ===> https://www.aspsnippets.com/Articles/jQuery-AJAX-Async-False-Synchronous-call-and-Async-True-Asynchronous-call-difference.aspx
  6. 要了解更多信息,请参考这个链接=== => https://www.aspsnippets.com/articles/jquery - ajax -异步-同步-异步-真实-异步-异步-异步-异步-区分-调用。aspx

#3


0  

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?

ajax()方法的异步选项已被弃用,现在怎么办?

#4


0  

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 SetParamForToDoList will be excecuted first after the function ActionForm.EditActionFormPopup will fire.

在这里,SetParamForToDoList将在函数ActionForm之后首先被执行。EditActionFormPopup会火。

#1


26  

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:

您将异步设置为false,当您需要在浏览器传递到其他代码之前完成ajax请求:

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

#2


4  

  1. When async setting is set to false, a Synchronous call is made instead of an Asynchronous call.
  2. 当异步设置为false时,将发出同步调用而不是异步调用。
  3. 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.
  4. 当jQuery AJAX函数的异步设置为true时,将进行jQuery异步调用。AJAX本身意味着异步JavaScript和XML,因此如果通过将异步设置为false使其同步,它将不再是AJAX调用。
  5. for more information please refer this link ===> https://www.aspsnippets.com/Articles/jQuery-AJAX-Async-False-Synchronous-call-and-Async-True-Asynchronous-call-difference.aspx
  6. 要了解更多信息,请参考这个链接=== => https://www.aspsnippets.com/articles/jquery - ajax -异步-同步-异步-真实-异步-异步-异步-异步-区分-调用。aspx

#3


0  

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?

ajax()方法的异步选项已被弃用,现在怎么办?

#4


0  

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 SetParamForToDoList will be excecuted first after the function ActionForm.EditActionFormPopup will fire.

在这里,SetParamForToDoList将在函数ActionForm之后首先被执行。EditActionFormPopup会火。