Get the App
SLTechnology News&Howtos  ›  Development  › 

How to build a pool of XMLHttpRequest objects

Shulou Source: shulou.com Published: 2022-06-03 07:18:50 09月30日 Update

This article mainly introduces "how to build a XMLHttpRequest object pool". In daily operation, I believe many people have doubts about how to build a XMLHttpRequest object pool. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to build a XMLHttpRequest object pool". Next, please follow the editor to study!

In ajax applications, a page usually sends multiple requests at the same time. If there is only one XMLHttpRequest object and the previous request is not completed, the previous request will be overwritten. If a new XMLHttpRequest object is created each time, it will also cause waste. The solution is to create a pool of XMLHttpRequset objects, and use this object if there are free objects in the pool, otherwise a new object will be created.

Here is a simple class I wrote recently:

The code is as follows:

/ * *

* XMLHttpRequest Object Pool

*

* @ author legend

* @ link http://www.ugia.cn/?p=85

* @ Copyright www.ugia.cn

, /

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]; } } // IE5中不支持push方法 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) { } } } // mozilla某些版本没有readyState属性 if (objXMLHttp.readyState == null) { objXMLHttp.readyState = 0; objXMLHttp.addEventListener("load", function () { objXMLHttp.readyState = 4; if (typeof objXMLHttp.onreadystatechange == "function") { objXMLHttp.onreadystatechange(); } }, false); } return objXMLHttp; }, // 发送请求(方法[post,get], 地址, 数据, 回调函数) sendReq: function (method, url, data, callback) { var objXMLHttp = this._getInstance(); with(objXMLHttp) { try { // 加随机数防止缓存 if (url.indexOf("?") >

0)

{

Url + = "& randnum=" + Math.random ()

}

Else

{

Url + = "? randnum=" + Math.random ()

}

Open (method, url, true)

/ / set the request encoding method

SetRequestHeader ('Content-Type',' application/x-www-form-urlencoded; charset=UTF-8')

Send (data)

Onreadystatechange = function ()

{

If (objXMLHttp.readyState = = 4 & & (objXMLHttp.status = = 200 | | objXMLHttp.status = = 304))

{

Callback (objXMLHttp)

}

}

}

Catch (e)

{

Alert (e)

}

}

}

}

Example:

The code is as follows:

Function test (obj)

{

Alert (obj.statusText)

}

XMLHttp.sendReq ('GET',' http://www.ugia.cn/wp-data/test.htm',', test)

XMLHttp.sendReq ('GET',' http://www.ugia.cn/wp-data/test.htm',', test)

XMLHttp.sendReq ('GET',' http://www.ugia.cn/wp-data/test.htm',', test)

XMLHttp.sendReq ('GET',' http://www.ugia.cn/wp-data/test.htm',', test)

Alert ('Pool length:' + XMLHttp._objPool.length)

At this point, the study on "how to build a pool of XMLHttpRequest objects" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Objects learning methods code more help practical next functions methods only at the same time addresses multiple that is attributes data articles methods versions Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Shulou Information OPPO Reno Linux Xiaomi