zoukankan      html  css  js  c++  java
  • 浏览器内存泄露问题

    场景描述:

      一个用于交易的web客户端,浏览器需要每一秒都需要postback,获取最新的行情。

    用window.setInterval("function",1000); 

    function 用jquery的ajax方法。

     $.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) {
    }});                                                            
    

      注意:

        一定要在complete方法中将jqXHR(xmlHttpRequset)对象设置null,否则内存会越来越大。


    由于每一次数据回来之后,都需要删除原来的数据,然后重新复制。

    代码如下:

    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增加到几百M

    修改代码如下:

      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);

    这样修改后,所有浏览器都没有在发生内存泄露问题。

    这是今天所解决的项目问题,记录一下。

  • 相关阅读:
    CSS实现背景透明,文字不透明(兼容各浏览器)
    JQUERY SCROLL PATH自定义滚动路径
    Truffle3.0集成NodeJS并完全跑通(附详细实例,可能的错误)
    truffle的调用nodeJs的问题
    Truffle基础篇-Truffle做什么的?怎么安装?
    以太坊智能合约开发笔记
    day02 智能合约
    remix无法安装的解决方案
    基于eth快速发行自己的数字货币
    remix-ide的三种使用方式
  • 原文地址:https://www.cnblogs.com/Persistence/p/3451922.html
Copyright © 2011-2022 走看看