Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to build a pool of XMLHttpRequest objects

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report