



































































































通过建立xmlHttp池来有效避免多个ajax同时请求冲突的问题,可以复用空闲的xmlHttp对象避免为每个ajax请求生成新xmlHttp对象的问题
对以上代码可以简化一下,缩短长度
1
var XMLHttp = {
2
_objPool: [],
3
_getInstance: function ()
4
{
5
for (var i = 0; i < this._objPool.length; i ++)
6
{
7
if (this._objPool[i].readyState == 0 || this._objPool[i].readyState == 4)
8
{
9
return this._objPool[i];
10
}
11
}
12
this._objPool[this._objPool.length] = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
13
return this._objPool[this._objPool.length - 1];
14
},
15
sendReq: function (method, url, data, callback)
16
{
17
var objXMLHttp = this._getInstance();
18
with(objXMLHttp)
19
{
20
url += ((url.indexOf("?")>0) ? "&" : "?") + "r="+Math.random();
21
open(method, url, true);
22
send(data);
23
onreadystatechange = function ()
24
{
25
if (objXMLHttp.readyState == 4 && objXMLHttp.status == 200)
26
callback(objXMLHttp);
27
}
28
}
29
}
30
};

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30
