About Ajax connection pool

Source: Internet
Author: User
In many cases, Ajax xmlHttpRequest requests are not concurrent, so you can call them directly in JS or call methods in jquery;
However, if the request concurrency is considered, and the time is long and the frequency is high, the connection pool problem needs to be considered;
The following is the Ajax connection pool code:
Code:Var XMLHttp = {
_ ObjPool: [],
_ GetInstance: function (){
For (var I = 0; I <this. _ objPool. length; I ++ ){
If (this. _ objPool [I]. readyState = 0 | this. _ objPool [I]. readyState = 4 ){
Return this. _ objPool [I];
}
}
// The push method is not supported in IE5.
This. _ objPool [this. _ objPool. length] = this. _ createObj ();
Return this. _ objPool [this. _ objPool. length-1];
},
_ CreateObj: function (){
If (window. XMLHttpRequest ){
Var objXMLHttp = new XMLHttpRequest ();
}
Else {
Var MSXML = ['msxml2. XMLHTTP.5.0 ', 'msxml2. XMLHTTP.4.0', 'msxml2. XMLHTTP.3.0 ', 'msxml2. xmlhttp', 'Microsoft. xmlhttp'];
For (var n = 0; n <MSXML. length; n ++ ){
Try {
Var objXMLHttp = new ActiveXObject (MSXML [n]);
Break;
}
Catch (e ){
}
}
}
// Some mozilla versions do not have the readyState attribute
If (objXMLHttp. readyState = null ){
ObjXMLHttp. readyState = 0;
ObjXMLHttp. addEventListener ("load", function (){
ObjXMLHttp. readyState = 4;
If (typeof objXMLHttp. onreadystatechange = "function "){
ObjXMLHttp. onreadystatechange ();
}
}, False );
}
Return objXMLHttp;
},
// Send the request (method [post, get], address, Data, callback function)
SendReq: function (method, url, data, callback ){
Var objXMLHttp = this. _ getInstance ();
With (objXMLHttp ){
Try {
// Add random numbers to prevent caching
If (url. indexOf ("? ")> 0 ){
Url + = "& randnum =" + Math. random ();
}
Else {
Url + = "? Randnum = "+ Math. random ();
}
Open (method, url, true );
Setrequestheader ("cache-control", "no-cache"); // The request page is not cached.
SetRequestHeader ('content-type', 'application/x-www-form-urlencoded; charset = UTF-8 '); // sets the Request Encoding Method
Send (data );
Onreadystatechange = function (){
If (objXMLHttp. readyState ==4 & (objXMLHttp. status == 200 | objXMLHttp. status == 304 )){
Callback (objXMLHttp );
Url = null;
Data = null;
Callback = null;
Method = null;
CollectGarbage ();
}
}
}
Catch (e ){
Alert ("Script Error:" + e );
}
}
}
};

Call example:

// Send information
Function sendMessage (username, uid, data1, user_to ){
If (isNaN (uid) return;
Var data = "[data] =" + escape (data1 );
Var url = "Ajax_method.aspx? Method = sendMessage & id = "+ uid +" & username = "+ escape (username) +" & user_to = "+ user_to;
XMLHttp. sendReq ("post", url, data, function (request ){
Var state = request. responseText;
If (state = "shibai "){
Alert ("sending information failed ...");
}
});
}

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.