瀏覽器內存泄露問題

場景描述: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);

這樣修改後,全部瀏覽器都沒有在發生內存泄露問題。

這是今天所解決的項目問題,記錄一下。

相關文章
相關標籤/搜索