Ajax Synchronous and asynchronous

Source: Internet
Author: User

In Web script programming, you should use asynchronous mode in most cases; synchronization mode will suspend the current scripting engine, so when you use sync mode, you should know what you want. In C + + development, synchronization mode should be the mainstream, if you must use asynchronous mode plus callback, you can refer to the using Ixmlhttprequest onreadystatechange from C + +.
The following is the use of asynchronous mode to obtain the remote host RSS file code, the key is to set a callback function to Ixmlhttprequest::onreadystatechange. To prevent the script from prematurely retreating to the console, the Asyncdone variable is used to detect the current state. Of course, if you use XMLHTTP in a Web page, you don't have to bother--as long as the IE Web page doesn't close, the callback function doesn't quit.
var xmlhttp = new ActiveXObject ("MSXML2. xmlhttp.6.0 ");
var url = "http://www.cxybl.com/";
var asyncdone = false;
try {
Xmlhttp.open ("Get", url, True);
Xmlhttp.onreadystatechange = onreadystatechange;
Xmlhttp.send (NULL);
Loop so this program from quiting
while (!asyncdone) {
Wscript.Sleep (100);
}
WScript.Echo (Xmlhttp.responsetext);
catch (e) {
WScript.Echo (e);
}
function onreadystatechange () {
WScript.Echo ("ReadyState:" + xmlhttp.readystate);
if (xmlhttp.readystate = = 4) {
Asyncdone = true;
}
}
Synchronization mode Gets the code for a remote host resource much simpler:

var xmlhttp = new ActiveXObject ("MSXML2. xmlhttp.6.0 ");
var url = "http://www.cxybl.com/";
try {
Xmlhttp.open ("Get", url, false);
Xmlhttp.send (NULL);
WScript.Echo (Xmlhttp.responsetext);
catch (e) {
WScript.Echo (e);
}
However, if you use sync mode in IE, the script is suspended until XMLHTTP returns because there is no callback mechanism and IE does not support script threads. Note that the IE interface itself will be suspended.
Synchronous or asynchronous, specific issues to be analyzed.



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.