Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between async:false and async:true in jquery ajax?

In jquery ajax there is a parameter

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

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

like image 706
Brutal_JL Avatar asked Nov 26 '13 05:11

Brutal_JL


People also ask

What is async false in AJAX?

Setting async to false means that the statement you are calling has to complete before the next statement in your function can be called. If you set async: true then that statement will begin it's execution and the next statement will be called regardless of whether the async statement has completed yet.

What is the meaning of async in AJAX?

Asynchronous (in Ajax) processes incoming requests in a constant event stack and sends small requests one after the other without waiting for responses. In other words, asynchronous ajax call allow the next line of code to execute, whereas synchronous call stop JavaScript execution until the response from server.

What is difference between sync and async in AJAX call explain with example?

AJAX: Synchronous or AsynchronousSynchronously, in which the script stops and waits for the server to send back a reply before continuing. Asynchronously, in which the script allows the page to continue to be processed and handles the reply if and when it arrives.

Is Ajax async false deprecated?

As of jQuery 1.8, the use of async:false in jQuery. ajax() is deprecated.


1 Answers

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> 

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 ) – Script stops and waits for the server to send back a reply before continuing. There are some situations where Synchronous Ajax is mandatory.

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.

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

Asynchronous and Synchronous AJAX calls

like image 157
Nidhish Krishnan Avatar answered Oct 10 '22 22:10

Nidhish Krishnan