⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xhr.js

📁 ajax快速入门,主要讲解原理与部门实现
💻 JS
字号:
var URL_PROXY="http://localhost/ajax/GetRawFile.php?url=";

if (window.ActiveXObject && !window.XMLHttpRequest) {
  window.XMLHttpRequest = function() {
    var MSXML = ['Msxml2.XMLHTTP.5.0','Msxml2.XMLHTTP.4.0','Msxml2.XMLHTTP.3.0','Msxml2.XMLHTTP','Microsoft.XMLHTTP'];
    for (var i = 0; i < MSXML.length; i++) {
      try {
        return new ActiveXObject(MSXML[i]);
      } catch (e) {}
    }
    return null;
  };
}

function _XMLHttpRequest(){
    this.xhr=new XMLHttpRequest();	
}

_XMLHttpRequest.prototype.setRequestHeader=function(contentType,valueStr){
	this.xhr.setRequestHeader(contentType,valueStr);
}

_XMLHttpRequest.prototype.open=function(method,url){
	this.busy=true;
	this.xhr.onreadystatechange = wrapCallback(this);
	if(url.substring(0,7)=="http://"){
		url = URL_PROXY + url;
	}
	if(method=="GET"){
		url += (url.indexOf("?")==-1?"?":"&") + "timestamp=" + new Date().getTime();
	}
	putLog(this.parentName+".url:"+url+"<br>");
	this.xhr.open(method,url);
}

_XMLHttpRequest.prototype.send=function (data) {
	putLog(this.parentName+".send:"+data+"<br>");
    return this.xhr.send(data);
}

wrapCallback = function (_this) {
    return function () {
		try{
			_this.readyState=_this.xhr.readyState;
			if (_this.readyState==4) {
				_this.status=_this.xhr.status;
				if(_this.status==200){
					_this.responseXML = _this.xhr.responseXML;
					_this.responseText = _this.xhr.responseText;
				}
			}
			_this.onreadystatechange();
			if (_this.readyState==4&&_this.status==200){
				putLog(_this.parentName+":false<br>");				
				_this.busy=false;
			}
		}catch(e){
			putLog(_this.parentName+".e:"+e+"<br>");
			putLog(_this.parentName+":false<br>");				
			_this.busy=false;
		}
	}
}

function httpRequest(method,url,header,data,callback,tryAgian){
	this.method=method;
	this.url=url;
	this.header=header;
	this.data=data;
	this.callback=callback;
	this.tryAgian=tryAgian;
}

function httpRequestManager(xhrNumber,objName,timeLength){
	this.xhrNumber=xhrNumber;
	this.xhrArray=new Array();
	for(var i=0;i<xhrNumber;i++){
		this.xhrArray[i]=new _XMLHttpRequest();
		this.xhrArray[i].parentName=objName;
		this.xhrArray[i].busy=false;
	}
	this.objName=objName;
	this.timeLength=timeLength;
	this.taskQueue=new Array();
}

httpRequestManager.prototype.getFreeXHR=function(){
	for(var i=0;i<this.xhrNumber;i++){
		if(this.xhrArray[i].busy==false){
			return this.xhrArray[i];
		}
	}
	return null;
}

httpRequestManager.prototype.addTask=function(hr){
	this.taskQueue.push(hr);
}

httpRequestManager.prototype.doTask=function(){
	var xhr=this.getFreeXHR();
	if(this.taskQueue.length>0&&xhr!=null){
		var hr=this.taskQueue.shift();
		xhr.onreadystatechange=hr.callback;
		try{
			xhr.open(hr.method,hr.url);
			if(hr.header!=null){
				xhr.setRequestHeader(hr.header.name,hr.header.value);
			}			
			xhr.send(hr.data);
		}catch(e){
			putLog(this.objName+".doTask.e:"+e+"<br>");
			if(hr.tryAgian==true){
				this.taskQueue.push(hr);
			}
		}
	}
}

httpRequestManager.prototype.start=function(){
	setInterval(this.objName+".doTask()",this.timeLength);
}

httpRequestManager.prototype.stop=function(){
	clearInterval();
}

var xmlHttpRequestManager=new httpRequestManager(10,"xmlHttpRequestManager",100);
xmlHttpRequestManager.start();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -