我做了一个聊天室...即时刷新的.
前台我写的是隔5秒请求后台查询数据库有最新的留言.然后我也写了一个针对IE的内存清理代码.这个是隔5秒清除一个settimeoutfunction ajax_loadTable(){
  //中间还有很多代码
if(settimeout_id!=null && settimeout_id!="")
  {
      clearTimeout(settimeout_id);  //清除settimeout函數產生的內存消耗.
  }
  settimeout_id=setTimeout("ajax_loadTable()",5000); //設置自動刷新.
}这个是清理IE内存垃圾的$(doucment).ready(function(){
if(isIE){CollectGarbage("collect");} //清除運行之前的內存.
  // 每隔 10 秒释放一次内存
if(isIE){window.setTimeout("CollectGarbage();",5000);}
ajax_loadTable();
});
问题就是:如何保证及时刷新 而且又能释放内存.不会产生内存溢出...

解决方案 »

  1.   

    用php么?如果是的话,请to me
      

  2.   

    注意数据库//a.html(发送消息)<textarea id="msg" rows="5" cols="30"></textarea>
    <input type="button" value="发送" onclick="send()"><span id="sendmsg"></span><script src="../jquery.js"></script>
    <script>
    function send(){
    var msg = document.getElementById("msg").value;
    if(!msg)document.getElementById("sendmsg").innerHTML = "消息不能为空";
    document.getElementById("sendmsg").innerHTML = "正在发送...";
    $.post("server.php", {"msg": msg, "rand": Math.random()}, function(data){
    if(data == "1"){
    document.getElementById("sendmsg").innerHTML = "发送成功!";
    }else{
    document.getElementById("sendmsg").innerHTML = "发送失败!";
    }
    });
    }
    </script>//b.html<div id="div"></div><script src="../jquery.js"></script>
    <script>
    function text_replace(text){
    var l = text.length;
    for (var i = 0; i < l; i++) {
    if (text.charAt(i) == "%"){
    if(text.charAt(i+1) == "2"){
    if(text.charAt(i+2) == "3"){
    text = text.replace("%23","#");
    }
    if(text.charAt(i+2) == "4"){
    text = text.replace("%24","$");
    }
    if(text.charAt(i+2) == "6"){
    text = text.replace("%26","&&");
    }
    if(text.charAt(i+2) == "B"){
    text = text.replace("%2B","+");
    }
    if(text.charAt(i+2) == "C"){
    text = text.replace("%2C",",");
    }
    if(text.charAt(i+2) == "F"){
    text = text.replace("%2F","/");
    }
    }
    if(text.charAt(i+1) == "3"){
    if(text.charAt(i+2) == "F"){
    text = text.replace("%3F","?");
    }
    if(text.charAt(i+2) == "B"){
    text = text.replace("%3B",";");
    }
    if(text.charAt(i+2) == "D"){
    text = text.replace("%3D","=");
    }
    if(text.charAt(i+2) == "A"){
    text = text.replace("%3A",":");
    }
    }
    if(text.charAt(i+1) == "4"){
    if(text.charAt(i+2) == "0"){
    text = text.replace("%40","@");
    }
    }
    }
    }
    return text;
    }var filemtime = 0;
    function test(){
    $.post("server.php", {"filemtime": filemtime, "rand": Math.random()}, function(data){
    if(data){
    data = eval("(" + data + ")");
    filemtime = data["filemtime"];
    document.getElementById("div").innerHTML = text_replace(decodeURI(data["msg"]));
    setTimeout("test()", 1000);
    }else{
    alert("error");
    }
    });
    }test();
    </script>//server.php<?php
    function DBConnect(){
    mysql_connect('192.168.1.2', 'db_user', 'db_pass');
    mysql_select_db(db_name');
    mysql_query('set names gbk');
    }if(isset($_POST['msg'])){
    $msg = iconv('utf-8', 'gbk', htmlspecialchars($_POST['msg']));
    DBConnect();
    mysql_query('insert into test(msg) values("'.$msg.'")');
    if(mysql_insert_id()){
    $fp = fopen('msgfile.txt', 'a');
    fputs($fp, $msg, 1);
    echo '1';
    exit;
    }
    echo '2';
    exit;
    }$filemtime = isset($_POST['filemtime']) && $_POST['filemtime']? $_POST['filemtime']: filemtime('msgfile.txt');while(1){
    if($filemtime == filemtime('msgfile.txt')){
    sleep(1);
    clearstatcache();
    }else{
    DBConnect();
    $rs = mysql_query('select msg from test order by id desc limit 0, 1');
    if($row = mysql_fetch_assoc($rs)){
    $msg = rawurlencode(iconv('gbk', 'utf-8', $row['msg']));
    }
    echo json_encode(array('filemtime' => filemtime('msgfile.txt'), 'msg' => $msg));
    exit;
    }
    }
    ?>
      

  3.   

    CollectGarbag这个API没什么用吧,刷新的时候自动回收
    如果存在IE内存不断增长得情况,则是你的代码中存在内存泄漏
    注意检查一下是否在闭包中定义Dom相应事件(onclick,onchange)如果有情在页面跳转前结束饮用
    比如obj.onclick =null等
      

  4.   

    //a.html(发送消息页)<textarea id="msg" rows="5" cols="30"></textarea>
    <input type="button" value="发送" onclick="send()"><span id="sendmsg"></span><script src="../jquery.js"></script>
    <script>
    function send(){
        var msg = document.getElementById("msg").value;
        if(!msg)document.getElementById("sendmsg").innerHTML = "消息不能为空";
        document.getElementById("sendmsg").innerHTML = "正在发送...";
        $.post("server.php", {"msg": msg, "rand": Math.random()}, function(data){
            if(data == "1"){
                document.getElementById("sendmsg").innerHTML = "发送成功!";
            }else{
                document.getElementById("sendmsg").innerHTML = "发送失败!";
            }
        });
    }
    </script>//b.html(消息接收页)<div id="div"></div><script src="../jquery.js"></script>
    <script>//上面调用的jquery(主要是用在ajax请求上,你也可以自己写ajax)
    function text_replace(text){//替换特殊字符,不用看
        var l = text.length;
        for (var i = 0; i < l; i++) {
            if (text.charAt(i) == "%"){
                if(text.charAt(i+1) == "2"){
                    if(text.charAt(i+2) == "3"){
                        text = text.replace("%23","#");
                    }
                    if(text.charAt(i+2) == "4"){
                        text = text.replace("%24","$");
                    }
                    if(text.charAt(i+2) == "6"){
                        text = text.replace("%26","&&");
                    }
                    if(text.charAt(i+2) == "B"){
                        text = text.replace("%2B","+");
                    }
                    if(text.charAt(i+2) == "C"){
                        text = text.replace("%2C",",");
                    }
                    if(text.charAt(i+2) == "F"){
                        text = text.replace("%2F","/");
                    }
                }
                if(text.charAt(i+1) == "3"){
                    if(text.charAt(i+2) == "F"){
                        text = text.replace("%3F","?");
                    }
                    if(text.charAt(i+2) == "B"){
                        text = text.replace("%3B",";");
                    }
                    if(text.charAt(i+2) == "D"){
                        text = text.replace("%3D","=");
                    }
                    if(text.charAt(i+2) == "A"){
                        text = text.replace("%3A",":");
                    }
                }
                if(text.charAt(i+1) == "4"){
                    if(text.charAt(i+2) == "0"){
                        text = text.replace("%40","@");
                    }
                }
            }
        }
        return text;
    }var filemtime = 0;//用于接收php页面返回对应文件的修改时间,然后再通过ajax传给php,如此循环
    function test(){
        $.post("server.php", {"filemtime": filemtime, "rand": Math.random()}, function(data){
            if(data){
                data = eval("(" + data + ")");
                filemtime = data["filemtime"];
                document.getElementById("div").innerHTML = text_replace(decodeURI(data["msg"]));
                setTimeout("test()", 1000);//每隔1秒
            }else{
                alert("error");
            }
        });
    }test();
    </script>//server.php(数据处理页面)<?php
    function DBConnect(){
        mysql_connect('192.168.1.2', 'db_user', 'db_pass');
        mysql_select_db('db_name');
        mysql_query('set names gbk');
    }if(isset($_POST['msg'])){
        $msg = iconv('utf-8', 'gbk', htmlspecialchars($_POST['msg']));
        DBConnect();
        mysql_query('insert into test(msg) values("'.$msg.'")');
        if(mysql_insert_id()){
            $fp = fopen('msgfile.txt', 'a');
            fputs($fp, $msg, 1);
            echo '1';
            exit;
        }
        echo '2';
        exit;
    }$filemtime = isset($_POST['filemtime']) && $_POST['filemtime']? $_POST['filemtime']: filemtime('msgfile.txt');while(1){
        if($filemtime == filemtime('msgfile.txt')){//如果文件时间未被修改(没有新消息)则请求再次睡眠(不用再次发送请求了)
            sleep(1);
            clearstatcache();
        }else{//如果文件时间被修改(有新消息),返回请求(睡眠的请求)
            DBConnect();
            $rs = mysql_query('select msg from test order by id desc limit 0, 1');
            if($row = mysql_fetch_assoc($rs)){
                $msg = rawurlencode(iconv('gbk', 'utf-8', $row['msg']));
            }
            echo json_encode(array('filemtime' => filemtime('msgfile.txt'), 'msg' => $msg));
            exit;
        }
    }
    ?>
      

  5.   


    var filemtime = 0;//用于接收php页面返回对应文件的修改时间,然后再通过ajax传给php,如此循环
    function test(){
        $.post("server.php", {"filemtime": filemtime, "rand": Math.random()}, function(data){
            if(data){
                data = eval("(" + data + ")");
                filemtime = data["filemtime"];
                document.getElementById("div").innerHTML = text_replace(decodeURI(data["msg"]));
                setTimeout("test()", 1000);//每隔1秒
            }else{
                alert("error");
            }
        });
    }test();你这里会内存溢出吧....
      

  6.   

    我是按JavaScript对象来写的...按道理来说我跳开这个页面 我的所有这个页面的js就无效了吧?
      

  7.   


    whlie(1){
    echo 1;
    sleep(1);
    }
    上面的代码(执行不一定正确),楼主是否认为会出现内存溢出的情况呢?
      

  8.   

    我就是想能不能用JavaScript來决绝setTimeout的内存溢出...
      

  9.   

    JavaScript的大神呢????
    我就是想知道你们是怎么样处理setTimeout的内存溢出的....
      

  10.   


    我查询了就是setTimeout的问题...我的其他代码不会溢出...
      

  11.   

    你在setTimeout中改变了Dom节点的innerHTML,又没有把对应的onclick.onchange之类的置为null阿,
      

  12.   

    你可以用下载新版javascript memory leak detector工具:http://blogs.msdn.com/gpde/pages/javascript-memory-leak-detector-v2.aspx
    检测一下你的网页是否存在DOM 事件泄露
      

  13.   

    在AJAX返回成功后清理就可以保证了
      

  14.   

    设置为null...那我要用的时候怎么办呢?
    而且还有很多事件呢..
      

  15.   

    我现在变成这样的写法
    function a()
    {
      $.ajax({
        url:""....
        dataType:"json",
        success:function(data){b(data);}
       })
    }
    function b(table_data)
    {
       var str="";
       var settimeout_id;
       $("div").empty();
       for(var i=0;i<table_data.length;i++)
       {
          str+=table_data[i].message;....
       }
       $("div").prepend(str);
                if(settimeout_id!=null || settimeout_id!="")
                {                clearTimeout(settimeout_id);  //清除settimeout函數產生的內存消耗.
                    // 每隔 5 秒释放一次内存
                    settimeout_id=null;
                    str=null;
                    if(isIE){
                        try{
                                CollectGarbage();
                            }
                            catch(e){
                                alert("sasas");
                            }
                    }
                }
                settimeout_id=setTimeout("a()",5000); //設置自動刷新.
    }
      

  16.   

    应该是ajax代码有问题,我的跟本没有用过什么内存清理,开一整天也没有什么问题,你还是找找其它的ajax代码试试。
      

  17.   

    我的意思是在你修改html之前把Dom和其子节点的事件置为null,如果事件很多
    你可以用微软的工具看一下,使那些事件引起泄露
      

  18.   

    你们自己用sIEve-0.0.8
    测试IE内存溢出工具测试下...什么没有溢出...就算是用了clearTimeout一样会.