场景描述:javascript
一个用于交易的web客户端,浏览器须要每一秒都须要postback,获取最新的行情。java
用window.setInterval("function",1000); jquery
function 用jquery的ajax方法。web
$.ajax( { type: "GET", url: "url" cache: false, dataType: "json", error: function (jqXHR, textStatus, errorThrown) { //发生错误 clearInterval(refreshHq); // alert(jqXHR); }, complete: function (jqXHR, TS) { jqXHR= null }, success: function (data, textStatus) { }});
注意:ajax
必定要在complete方法中将jqXHR(xmlHttpRequset)对象设置null,不然内存会愈来愈大。json
因为每一次数据回来以后,都须要删除原来的数据,而后从新复制。浏览器
代码以下:app
parent.empty(); var newtemp = template.replace("{0}", jsonob[i].Commodity.CommodityName).replace("{1}", jsonob[i].SellPrice.toFixed(2).N()).replace("{2}", jsonob[i].BuyPrice.toFixed(2).N()).replace("{3}", jsonob[i].High.toFixed(2).N()).replace("{4}", jsonob[i].Low.toFixed(2).N()) if (i % 2 == 0) { parent.append("<li class=\"youBJ\" id=\"" + jsonob[i].CommodityID + "\"> " + newtemp + "</li>"); } else { parent.append("<li id=\"" + jsonob[i].CommodityID + "\">" + newtemp + "</li>"); }
可是在IE8下,内存依然在增加。从几十M增长到几百Mpost
修改代码以下:url
var newtemp = template.replace("{0}", jsonob[i].Commodity.CommodityName).replace("{1}", jsonob[i].SellPrice.toFixed(2).N()).replace("{2}", jsonob[i].BuyPrice.toFixed(2).N()).replace("{3}", jsonob[i].High.toFixed(2).N()).replace("{4}", jsonob[i].Low.toFixed(2).N()); var element = document.createElement("li"); element.id = jsonob[i].CommodityID; element.innerHTML = newtemp; if (i % 2 == 0) { element.className = "youBJ"; } parent[0].appendChild(element);
这样修改后,全部浏览器都没有在发生内存泄露问题。
这是今天所解决的项目问题,记录一下。