ajax 8秒刷新一次,当打开多个ie后,内存都被吃光。有没有方法解决内存问题?

发布时间:2007年03月19日      浏览次数:1590 次
我遇到了同样的问题,经过一天的寻找,从网上找了段代码,我提供处来,经过对这段代码改造后,内存将不会再持续增加.这段代码楼主自己可以改造成适合自己的,其核心不占用内存的思路就是:
YidingHe(机枪兵) ( ) 信誉:100 Blog 2006-11-9 10:59:06 得分: 0
使用同一个 XMLHttpRequest 对象进行刷新,而不是每次刷新就新建一个。
代码如下:
var requests = new Array();
var blnIEAjax = false;
if(typeof(XMLHttpRequest) == 'undefined')
{
blnIEAjax=true;
var XMLHttpRequest = function()
{
var request = null;
try
{
request = new ActiveXObject('Msxml2.XMLHTTP');
}
catch(e)
{
try
{
request = new ActiveXObject('Microsoft.XMLHTTP');
}
catch(ee)
{
}
}
return request;
}
}
function ajax_stop()
{
for(var i=0; i<requests.length; i++)
{
if(requests[i] != null)
requests[i].abort();
}
}
function ajax_create_request(context)
{
for(var i=0; i<requests.length; i++)
{
if (blnIEAjax)
{
if(requests[i].obj.readyState == 4)
{
requests[i].obj.abort();
requests[i].context = context;
return requests[i];
}
}else{
if(requests[i].readyState == 4)
{
requests[i].abort();
requests[i].context = context;
return requests[i];
}
}
}
var pos = requests.length;
requests[pos] = Object();
requests[pos].obj = new XMLHttpRequest();
requests[pos].context = context;
return requests[pos];
}
function ajax_request(url, data, callback, context)
{
var request = ajax_create_request(context);
var async = typeof(callback) == 'function';
if(async) request.obj.onreadystatechange = function()
{
if(request.obj.readyState == 4)
callback(new ajax_response(request));
}
request.obj.open('POST', url, async);
request.obj.send(data);
if(!async)
return new ajax_response(request);
}
function ajax_response(request)
{
this.request = request.obj;
this.error = null;
this.value = null;
this.context = request.context;
if(request.obj.status == 200)
{
try
{
this.value = object_from_json(request);
if(this.value && this.value.error)
{
this.error = this.value.error;
this.value = null;
}
}
catch(e)
{
this.error = new ajax_error(e.name, e.description, e.number);
}
}
else
{
this.error = new ajax_error('HTTP request failed with status: ' + request.obj.status, request.obj.status);
}
return this;
}
function enc(s)
{
return s.toString().replace(/\%/g, "%26").replace(/=/g, "%3D");
}
function object_from_json(request)
{
if(request.obj.responseXML != null && request.obj.responseXML.xml != null && request.obj.responseXML.xml != '')
return request.obj.responseXML;
var r = null;
eval('r=' + request.obj.responseText + ';');
return r;
}
function ajax_error(name, description, number)
{
this.name = name;
this.description = description;
this.number = number;
return this;
}
ajax_error.prototype.toString = function()
{
return this.name + " " + this.description;
}
function json_from_object(o)
{
if(o == null)
return 'null';
switch(typeof(o))
{
case 'object':
if(o.constructor == Array)// checks if it is an array [,,,]
{
var s = '';
for(var i=0; i<o.length; ++i)
{
s += json_from_object(o[i]);
if(i < o.length -1)
s += ',';
}
return '[' + s + ']';
}
break;
case 'string':
return '"' + o.replace(/(["\\])/g, '\\$1') + '"';
default:
return String(o);
}
}var ajaxVersion = '5.7.22.2';
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!