The difference analysis of Ajax synchronous request and asynchronous request _javascript skill

Source: Internet
Author: User
Ajax synchronization and asynchronous differences, first look at 2 pieces of code:
Code One:
Copy Code code as follows:

Synchronize = function (Url,param) {
function Createxhrobject () {
var http;
var ActiveX = [MSXML2. xmlhttp.3.0 "," MSXML2. XMLHTTP "," Microsoft.XMLHTTP "];
try {
http = new XMLHttpRequest;
catch (e) {
for (var i = 0; i < activex.length; ++i) {
try {
http = new ActiveXObject (Activex[i]);
Break
catch (e) {}
}
finally {
return HTTP;
}
}
var conn = Createxhrobject ();
Conn.Open ("POST", url, false);//ajax Sync
Conn.send (param);
var strreturn = Conn.responsetext;
Alert ("1");
if (Strreturn!= "") {
Return Ext.decode (Conn.responsetext);
} else {
return null;
}
Alert ("2");
};

Code two:
Ajax Synchronous Request Method:
Copy Code code as follows:

Synchronize = function (Url,param) {
function Createxhrobject () {
var http;
var ActiveX = [MSXML2. xmlhttp.3.0 "," MSXML2. XMLHTTP "," Microsoft.XMLHTTP "];
try {
http = new XMLHttpRequest;
catch (e) {
for (var i = 0; i < activex.length; ++i) {
try {
http = new ActiveXObject (Activex[i]);
Break
catch (e) {}
}
finally {
return HTTP;
}
}
var conn = Createxhrobject ();
Conn.Open ("POST", url, True);//ajax asynchronous
Conn.send (param);
var strreturn = Conn.responsetext;
Alert ("1");
if (Strreturn!= "") {
Return Ext.decode (Conn.responsetext);
} else {
return null;
}
Alert ("2");
};

The differences between synchronization and Asynchrony are as follows:

Conn.Open (' POST ', url,true); Ajax asynchronous
Conn.Open (' POST ', url,false); Ajax Sync

For code two, the result of the asynchronous AJAX request is: Execute alert (2) and then execute alert (1), which means that once the Conn.Open request is sent, the front end does not wait for its response to execute the following code, so alert (2) executes it first, Then alert (1) is executed when the response response arrives;

For code one, for synchronous AJAX requests, the execution result is: First execute alert (1) and then execute alert (2), which means that once the Conn.Open request is sent, the front end waits for its response, and after the response completes, alert (1) executes first and then alert ( 2);
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.