①图片切换内存泄露
使用更改图片背景方法,不要使用直接修改图片方法
document.getElementById(id).style.backgroundImage = "url(" + img + ")"; //不会泄露
document.getElementById(id).src= img ; //泄露
② 移除节点前先移除节点包含事件,如移除下面该SPAN节点时
<span title='1213' οnclick=callfunction('" + issi + "'); οncοntextmenu=‘function’ >" + issi + "</span>";//
for (var i = 0; i < this.childNodes.length; i++) {
if (this.childNodes[i].tagName == "SPAN") {
if (this.childNodes[i].oncontextmenu) {
this.childNodes[i].oncontextmenu = null; //事件禁止为空防止泄露
}
if (this.childNodes[i].onclick) {
this.childNodes[i].onclick = null;
this.childNodes[i] = null; //事件禁止为空防止泄露
}
//removeChildSafe(this.childNodes[i]);
}
}
this.innerHTML = "";
③多次AJAX异步请求时不要使用JQURY,使用原生态方法防止频繁生成XMLHTTP远程对象
function jquerygetNewData_ajax(url, pars, func, IsNotasynchronous, noProgressBar) {
var newxmlhttp = createHttp();
sendback(url, pars, func, newxmlhttp, "new");
newxmlhttp = null;
}
function LLHSuccesschange(func, xmlHttp, type) {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
var result = xmlHttp.responseText
if (type == "gis") {
$("#map div").each(function () {
if (this.id.indexOf("_vFigure") > -1) {
if (result != '') {
if (!isinGIS(this.ci, eval('(' + result + ')'))) {
$(this).fadeOut(1000, function () {
var AllLayersOutSide = document.getElementById(this.id);
var ul = document.getElementById("bztp_" + this.ci)
if (ul && useprameters.lockid != this.ci) {
removeChildSafe(ul);
}
removeChildSafe(AllLayersOutSide);
});
}
}
}
});
}
if (result != '') {
var classResult = eval('(' + result + ')');
func(classResult);
classResult = null;
}
result = null;
}
if (type == "new") {
delete xmlHttp["onreadystatechange"];
xmlHttp.abort();
xmlHttp.xmlHttpRequest = null;
xmlHttp = null;
CollectGarbage();
}
}
}
function sendback(url, pars, func, xmlHttp, type) {
try {
url = url.concat('?L=1');
for (var o in pars) {
url = url.concat('&').concat(o).concat('=').concat(escape(pars[o]));
}
if (type == "gis" && xmlHttp.readyState != 0 && xmlHttp.readyState != 4)
return;
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = function () {
try {
LLHSuccesschange(func, xmlHttp, type);
}
catch (e) {
}
}
xmlHttp.setRequestHeader("Content-Type", "application/json");
xmlHttp.setRequestHeader("Cache-Control", "no-cache"); //用get方式,得加上,这样就不会缓存
xmlHttp.setRequestHeader("If-Modified-Since", "0");
xmlHttp.send(null);
//创建XMLHttpRequest对象
}
catch (e) {
}
}
function createHttp() {
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e2) {
xmlHttp = false;
}
}
if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
xmlHttp = new XMLHttpRequest();
}
return xmlHttp;
}
④删除对象,将对象内容置空防止泄露
window.recycler = (function () { var t = document.createElement('div'); t.id = "recycler"; return t; })();
function removeChildSafe(el) {
if (!el) { return; }
if (el && window.recycler) {
for (prop in el) {
prop = null;
}
window.recycler.appendChild(el);
window.recycler.innerHTML = '';
}
}
⑤少用creatElement及removechild
可以预先创建元素替换元素内容,而不频繁使用生成节点元素及删除节点元素。防止内存泄露
⑤传递数据用post,不要用get方式
⑤IE滤镜容易引起内存泄露