if (!window.Global)
{
window.Global = new Object();
}
Global._RequestQueue = function()
{
this._requestDelegateQueue = new Array();
this._requestInProgress = 0;
this._maxConcurrentRequest = 2;
}
Global._RequestQueue.prototype =
{
enqueueRequestDelegate : function(requestDelegate)
{
this._requestDelegateQueue.push(requestDelegate);
this._request();
},
next : function()
{
this._requestInProgress --;
this._request();
},
_request : function()
{
if (this._requestDelegateQueue.length <= 0) return;
if (this._requestInProgress >= this._maxConcurrentRequest) return;
this._requestInProgress ++;
var requestDelegate = this._requestDelegateQueue.shift();
requestDelegate.call(null);
}
}
Global.RequestQueue = new Global._RequestQueue();
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Request Queue</title>
<script type="text/javascript" src="js/prototype-1.4.0.js"></script>
<script type="text/javascript" src="js/RequestQueue.js"></script>
<script language="javascript" type="text/javascript">
function requestWithoutQueue()
{
for (var i = 0; i < 10; i++)
{
new Ajax.Request(
url,
{
method: 'post',
onComplete: callback
});
}
function callback(xmlHttpRequest)
{
...
}
}
function requestWithQueue()
{
for (var i = 0; i < 10; i++)
{
var requestDelegate = function()
{
new Ajax.Request(
url,
{
method: 'post',
onComplete: callback,
onFailure: Global.RequestQueue.next,
onException: Global.RequestQueue.next
});
}
Global.RequestQueue.enqueueRequestDelegate(requestDelegate);
}
function callback(xmlHttpRequest)
{
...
Global.RequestQueue.next();
}
}
</script>
</head>
<body>
...
</body>
</html>
window._progIDs = [ 'Msxml2.XMLHTTP', 'Microsoft.XMLHTTP' ];
if (!window.XMLHttpRequest)
{
window.XMLHttpRequest = function()
{
for (var i = 0; i < window._progIDs.length; i++)
{
try
{
var xmlHttp = new _originalActiveXObject(window._progIDs[i]);
return xmlHttp;
}
catch (ex) {}
}
return null;
}
}
if (window.ActiveXObject)
{
window._originalActiveXObject = window.ActiveXObject;
window.ActiveXObject = function(id)
{
id = id.toUpperCase();
for (var i = 0; i < window._progIDs.length; i++)
{
if (id === window._progIDs[i].toUpperCase())
{
return new XMLHttpRequest();
}
}
return new _originaActiveXObject(id);
}
}
window._originalXMLHttpRequest = window.XMLHttpRequest;
window.XMLHttpRequest = function()
{
this._xmlHttpRequest = new _originalXMLHttpRequest();
this.readyState = this._xmlHttpRequest.readyState;
this._xmlHttpRequest.onreadystatechange =
this._createDelegate(this, this._internalOnReadyStateChange);
}
window.XMLHttpRequest.prototype =
{
open : function(method, url, async)
{
this._xmlHttpRequest.open(method, url, async);
this.readyState = this._xmlHttpRequest.readyState;
},
send : function(body)
{
var requestDelegate = this._createDelegate(
this,
function()
{
this._xmlHttpRequest.send(body);
this.readyState = this._xmlHttpRequest.readyState;
});
Global.RequestQueue.enqueueRequestDelegate(requestDelegate);
},
setRequestHeader : function(header, value)
{
this._xmlHttpRequest.setRequestHeader(header, value);
},
getResponseHeader : function(header)
{
return this._xmlHttpRequest.getResponseHeader(header);
},
getAllResponseHeaders : function()
{
return this._xmlHttpRequest.getAllResponseHeaders();
},
abort : function()
{
this._xmlHttpRequest.abort();
},
_internalOnReadyStateChange : function()
{
var xmlHttpRequest = this._xmlHttpRequest;
try
{
this.readyState = xmlHttpRequest.readyState;
this.responseText = xmlHttpRequest.responseText;
this.responseXML = xmlHttpRequest.responseXML;
this.statusText = xmlHttpRequest.statusText;
this.status = xmlHttpRequest.status;
}
catch(e){}
if (4 === this.readyState)
{
Global.RequestQueue.next();
}
if (this.onreadystatechange)
{
this.onreadystatechange.call(null);
}
},
_createDelegate : function(instance, method)
{
return function()
{
return method.apply(instance, arguments);
}
}
}
<!--[if IE]>
<script type="text/javascript" src="js/RequestQueue.js"></script>
<script type="text/javascript" src="js/FakeXMLHttpRequest.js"></script>
<![endif]-->
本文出自 “赵��” 博客,转载请与作者联系!