+91 90691 39140 | +1 253 214 3115 | info@hub4tech.com | hub4tech

Asynchronous - True or False?


AJAX stands for Asynchronous JavaScript and XML, and for the XMLHttpRequest object to behave as AJAX, the async parameter of the open () method has to be set to true like xhttp.open("GET", "ajax_test.asp", true);

Sending asynchronous requests is a huge improvement for web developers. Many of the tasks performed on the server are very time consuming. Before AJAX, this operation could cause the application to hang or stop.With AJAX, the JavaScript does not have to wait for the server response, but can instead:
  • execute other scripts while waiting for server response
  • deal with the response when the response ready

Async=true

While using async=true, we specify a function to execute when the response is ready in the onreadystatechange event:

xhttp.onreadystatechange = function () {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById ("demo").innerHTML = xhttp.responseText;
}};
xhttp.open ("GET", "ajax_info.txt", true);
xhttp.send ();

Async=false

To use async=false, we change the third parameter in the open () method to false:
xhttp.open ("GET", "ajax_info.txt", false);
Using async=false is not recommended since the request made will not be of AJAX type, but for a few small requests this can be ok.Remember that the JavaScript will not continue to execute, until the server response is ready. If the server is busy or slow, the application will hang or stop.When we use async=false, we don’t write an onreadystatechange function - just put the code after the send () statement:

xhttp.open ("GET", "ajax_info.txt", false);
xhttp.send();
document.getElementById("demo").innerHTML = xhttp.responseText;

Sample Code with async=”false”

Asynchronous - True or False


Is it Helpful?
Copyright ©2015 Hub4Tech.com, All Rights Reserved. Hub4Tech™ is registered trademark of Hub4tech Portal Services Pvt. Ltd.
All trademarks and logos appearing on this website are the property of their respective owners.
FOLLOW US