天天看點

Ajax 對象池,可同時發送多個Ajax請求,相容所有主流浏覽器。

椤圭????瑕?涓存?跺????锛?灏???浼???锛?浣?宸茶凍澶?搴?浠?澶ч?ㄥ??搴??ㄤ?锛??煎?瑰?娴?瑙??????BUG??姝ょ被涓????ヤ??????瑰?锛??ㄥ??渚???瀵矽薄?惰?劇疆绗?涓?涓????頒負澶т?0???闆氨浼?寮???锛?澶?璇鋒??跺???瓒?杩?杩?涓??闆?煎氨浼???瀛??伴????涓?绋????ц???

1

12

?

13

function

?

$$(id){

return

?

document.getElementById(id);}

14

function

?

xdh_ajax(_poolSize,_instanceName){

15

????

this

.ajaxPool =?

new

?

Array();

16

????

this

.queryArr =?

new

?

Array();

17

????

this

.instanceName =?

''

;

18

????

this

.poolSize = 0;

19

????

var

?

_nextNum = 0;

20

?

21

????

//??寤轟?涓?XMLhttp瀵矽薄

22

????

this

.createXMLhttp =?

function

(){

23

????????

if

(

typeof

(ajax.ajaxPool) ==?

null

) ajax.ajaxPool =?

new

?

Array();

24

????????

if

(window.XMLHttpRequest){

25

????????????

this

.ajaxPool[_nextNum] =?

new

?

XMLHttpRequest();

26

????????

}

else

?

if

(window.ActiveXObject){

27

????????????

this

.ajaxPool[_nextNum] =?

new

?

ActiveXObject(

"Microsoft.XMLHTTP"

);

28

????????

}

29

????????

_nextNum = _nextNum + 1;

30

????

};

31

?

32

????

//?????芥??/code>

33

????

this

.init =?

function

(_poolSize,_instanceName){

34

????????

if

(!isNaN(_poolSize) && _poolSize != 0)?

this

.poolSize = _poolSize;

35

????????

if

(

typeof

(_instanceName) ==?

'undefined'

){

36

????????????

alert(

'蹇?椤繪??瀹?瀹?渚???绉?

);

37

????????????

return

?

false

;

38

????????

}

else

?

this

.instanceName = _instanceName;

39

????

};

40

?

41

????

//?峰??涓?涓?XMLhttp瀵矽薄

42

????

this

.getInstance =?

function

(){

43

????????

for

(_i?

in

?

this

.ajaxPool){

44

????????????

if

(

this

.ajaxPool[_i].readyState == 0 ||?

this

.ajaxPool[_i].readyState == 4){

45

????????????????

this

.ajaxPool[_i].onreadystatechange =?

function

(){};

46

????????????????

return

?

this

.ajaxPool[_i];

47

????????????

}

48

????????

}

49

????????

if

(

this

.poolSize == 0 || _nextNum <?

this

.poolSize){

50

????????????

this

.createXMLhttp();

51

????????????

return

?

this

.ajaxPool[_nextNum - 1];

52

????????

}

else

{

53

????????????

return

?

-1;

54

????????

}

55

????????

return

?

false

;

56

????

};

57

?

58

????

//绔??蟲?ц?涓?涓???绔???璇鋒?

59

????

this

.queryRightAway =?

function

(_method,_url,_func,_data){

60

????????

if

(window.XMLHttpRequest){

61

????????????

var

?

_o =?

new

?

XMLHttpRequest();

62

????????

}

else

?

if

(window.ActiveXObject){

63

????????????

var

?

_o =?

new

?

ActiveXObject(

"Microsoft.XMLHTTP"

);

64

????????

}

65

????????

if

(_method ==?

'GET'

){

66

????????????

var

?

joinstr =?

'ts='

+Math.random();

67

????????????

joinstr = (_url.indexOf(

'?'

) > -1) ??

'&'

+joinstr :?

'?'

+joinstr;

68

????????????

_o.open(

'GET'

,_url+joinstr,

true

);

69

????????????

_o.send(

null

);

70

????????

}

else

?

if

(_method ==?

'POST'

){

71

????????????

var

?

_sendData =?

'value='

+encodeURIComponent(_data);

72

????????????

_o.open(

'POST'

,_url,

true

);

73

????????????

_o.setRequestHeader(

"Content-Type"

,

"application/x-www-form-urlencoded"

);

74

????????????

_o.send(_sendData);

75

????????

}

76

????????

if

(

typeof

(_func) !=?

'undefined'

?

&& _func !=?

''

){

77

????????????

if

(

typeof

(_func) ==?

'object'

){

78

????????????????

var

?

_param =?

''

;

79

????????????????

for

(

var

?

_i=1;_i<_func.length;_i++){

80

????????????????????

_param = _param +?

',"'

?

+ _func[_i] +?

'"'

;

81

????????????????

}

82

????????????????

_o.onreadystatechange =?

function

(){

if

(_o.readyState == 4){eval(

'_func[0](_o'

+_param+

');'

);}};

83

????????????

}

else

{

84

????????????????

_o.onreadystatechange =?

function

(){

if

(_o.readyState == 4){_func(_o);}};

85

????????????

}

86

????????

}

87

????

};

88

?

89

????

//灏?璇鋒????ュ?伴????涓?

90

????

this

.addQueryToArr =?

function

(_method,_url,_func,_data){

91

????????

this

.queryArr.push(Array(_method,_url,_func,_data));

92

????

};

93

?

94

????

//??寰?????涓???绗?涓?涓?璇鋒?

95

????

this

.getQueryFromArr =?

function

(){

96

????????

return

?

this

.queryArr.shift();

97

????

};

98

?

99

????

//妫??ュ?ㄥ???璇鋒????跺??杩????朵?璇鋒??ㄩ????涓?锛?????璇?浼????ц?????涓???璇鋒?锛?濡?娌℃?????ц???娆?/code>

100

????

this

.checkQuery =?

function

(_method,_url,_func,_data){

101

????????

var

?

_obj =?

this

.getInstance();

102

????????

if

(_obj ===?

false

)?

return

?

false

;

103

????????

if

(_obj === -1){

104

????????????

if

(

typeof

(_method) !=?

'undefined'

)

this

.addQueryToArr(_method,_url,_func,_data);

105

????????????

return

?

true

;

106

????????

}

107

????????

if

(

this

.queryArr.length != 0){

108

????????????

if

(

typeof

(_method) !=?

'undefined'

)

this

.addQueryToArr(_method,_url,_func,_data);

109

????????????

var

?

temp =?

this

.getQueryFromArr();

110

????????

}

else

{

111

????????????

var

?

temp = Array(_method,_url,_func,_data);

112

????????

}

113

????????

temp = Array(_obj,temp[0],temp[1],temp[2],temp[3]);

114

????????

return

?

temp;

115

????

};

116

?

117

????

//??寮??炬??/code>

118

????

//?? _method锛?璇鋒??瑰?

119

????

//????? _url锛?璇鋒?缃???

120

????

//???? _func锛???璋??芥?幫?濡??????扮?锛??d??扮???绗?涓?涓???绱?涓哄?芥?闆??锛??扮????朵???绱???涓鴻?ュ?芥?扮??????/code>

121

????

//???? _data锛?POST?規????版??锛?????浠?涓虹ず渚?锛?浣跨?ㄩ??淇???/code>

122

????

//_rightaway锛????????蟲?ц?璇鋒?

123

????

this

.query =?

function

(_method,_url,_func,_data,_rightaway){

124

????????

if

(_rightaway == 1){

125

????????????

this

.queryRightAway(_method,_url,_func,_data);

126

????????????

return

?

true

;

127

????????

}

128

????????

var

?

_q =?

this

.checkQuery(_method,_url,_func,_data);

129

????????

if

(_q ===?

false

)?

return

?

false

;

130

????????

if

(_q ===?

true

)?

return

?

true

;

131

????????

if

(_q[1] ==?

'GET'

){

132

????????????

var

?

joinstr =?

'ts='

+Math.random();

133

????????????

joinstr = (_q[2].indexOf(

'?'

) > -1) ??

'&'

+joinstr :?

'?'

+joinstr;

134

????????????

_q[0].open(

'GET'

,_q[2]+joinstr,

true

);

135

????????????

_q[0].send(

null

);

136

????????

}

else

?

if

(_q[1] ==?

'POST'

){

137

????????????

var

?

_sendData =?

'value='

+encodeURIComponent(_q[4]);

138

????????????

_q[0].open(

'POST'

,_q[2],

true

);

139

????????????

_q[0].setRequestHeader(

"Content-Type"

,

"application/x-www-form-urlencoded"

);

140

????????????

_q[0].send(_sendData);

141

????????

}

142

????????

var

?

callback = (

this

.poolSize != 0) ?

'if('

+

this

.instanceName+

'.queryArr.length != 0)'

+

this

.instanceName+

'.query();'

?

:?

''

;

143

????????

//2010-11-05锛?淇?姝e?ㄥ??璋??芥?頒腑?ц?璺寵漿?虹?拌??????璇?锛?ajax涓虹┖??涓???瀵矽薄锛?

144

????????

//callback += this.instanceName + '.release();';

145

????????

callback +=?

'if('

+

this

.instanceName+

'){'

?

+?

this

.instanceName +

'.release();}'

;

146

????????

if

(

typeof

(_q[3]) !=?

'undefined'

?

&& _q[3] !=?

''

){

147

????????????

if

(

typeof

(_q[3]) ==?

'object'

){

148

????????????????

var

?

_param =?

''

;

149

????????????????

for

(

var

?

_i=1;_i<_q[3].length;_i++){

150

????????????????????

_param = _param +?

',"'

?

+ _q[3][_i] +?

'"'

;

151

????????????????

}

152

????????????????

_q[0].onreadystatechange =?

function

(){

if

(_q[0].readyState == 4){eval(

'_q[3][0](_q[0]'

+_param+

');'

+callback);}};

153

????????????

}

else

{

154

????????????????

_q[0].onreadystatechange =?

function

(){

if

(_q[0].readyState == 4){eval(

'_q[3](_q[0]);'

+callback);}};

155

????????????

}

156

????????

}

157

????

};

158

?

159

????

//???懼?矽薄

160

????

this

.release =?

function

(isabort){

161

????????

for

(_i?

in

?

this

.ajaxPool){

162

????????????

if

(isabort == 1){

163

????????????????

this

.ajaxPool[_i].onreadystatechange =?

function

(){};

164

????????????????

this

.ajaxPool[_i].abort();

165

????????????

}

166

????????????

if

(

this

.ajaxPool[_i].readyState == 0 ||?

this

.ajaxPool[_i].readyState == 4){

167

????????????????

this

.ajaxPool.splice(_i,1);

168

????????????????

_nextNum = _nextNum - 1;

169

????????????

}

170

????????

}

171

????

};

172

?

173

????

//??濮???绫?/code>

174

????

this

.init(_poolSize,_instanceName);

175

}

176

?

177

//姝ゅ?芥?扮?ㄤ??ㄨ煩?烘?剁?姝㈣?鋒?

178

function

?

closewindow(){

179

????

if

(

typeof

(ajax) !=?

'undefined'

?

&& ajax !=?

null

){

180

????????

_isclose =?

true

;

181

????????

_unfinishedTotal = _unfinished = 0;

182

????????

while

(ajax.ajaxPool.length != 0){

183

????????????

ajax.release(1);

184

????????

}

185

????????

ajax =?

null

;

186

????

}

187

}

188

window.onbeforeunload = window.onunload = closewindow;

?ㄤ?????璋??芥?頒腑蹇?椤誨0???沖?涓?涓????幫?姝ゅ???頒負涓?涓?XMLHttpRequest瀵矽薄瀹?渚?锛??蜂?璇風??涓??㈢ず渚????

release?規?璇峰?ㄩ??褰????跺????琛?璋????浠ュ????澶????????

涓?涓?绠?????搴??ㄤ?瀛?锛??峰?????″?ㄦ?堕?淬???

xdh_reponse.php

1

<?php

2

die

(

date

(

'Y-m-d H:i:s'

));

3

?>

xdh_request.html

1

<

html

>

2

<

head

>

3

<

meta

?

http-equiv

=

"Content-Type"

?

content

=

"text/html; charset=gb2312"

?

/>

4

<

script

?

language

=

"javascript"

?

type

=

"text/javascript"

?

src

=

"xdh_ajax.js"

></

script

>

5

<

script

?

language

=

"javascript"

?

type

=

"text/javascript"

>

6

var ajax = new xdh_ajax(0,'ajax');

7

function update(_obj){

8

?

$$('serverTime').innerHTML = _obj.responseText;

9

}

10

function query(){

11

?

ajax.query('GET','xdh_reponse.php',update);

12

}

13

</

script

>

14

<

title

>?峰???堕??lt;/

title

>

15

</

head

>

16

?

17

<

body

>

18

<

div

?

id

=

"serverTime"

></

div

>

19

<

input

?

type

=

"button"

?

onclick

=

"query()"

?

value

=

"?存??

?

/>

20

</

body

>

21

</

html

>

繼續閱讀