我做了一个聊天室...即时刷新的.
前台我写的是隔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();
});
问题就是:如何保证及时刷新 而且又能释放内存.不会产生内存溢出...
前台我写的是隔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();
});
问题就是:如何保证及时刷新 而且又能释放内存.不会产生内存溢出...
解决方案 »
- 关于<marquee>的一个小问题
- |zyciis| 我给Jquery插件FancyBox添加拖动的效果,但效果不好,拖快了就不行了,麻烦大家帮我看看,谢谢 有源码
- ext.FormPanel.load 为什么总是failure
- 我要问个问题,问题如下:
- 表格列宽拖动问题!!!
- 只能输入数字的并且能响应回车事件的文本框
- 求救
- 怎样用javascript来写密码验证
- 问个窗口的函数?在线给分!
- uploadify 上传图片 请求多了个404 路径?还有两次多余的请求uploadify.swf?preventswfcaching=?如何只有一次请求呢?
- JavaScript新手的问题
- JS 字体放大缩小
<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;
}
}
?>
如果存在IE内存不断增长得情况,则是你的代码中存在内存泄漏
注意检查一下是否在闭包中定义Dom相应事件(onclick,onchange)如果有情在页面跳转前结束饮用
比如obj.onclick =null等
<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;
}
}
?>
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();你这里会内存溢出吧....
whlie(1){
echo 1;
sleep(1);
}
上面的代码(执行不一定正确),楼主是否认为会出现内存溢出的情况呢?
我就是想知道你们是怎么样处理setTimeout的内存溢出的....
我查询了就是setTimeout的问题...我的其他代码不会溢出...
检测一下你的网页是否存在DOM 事件泄露
而且还有很多事件呢..
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); //設置自動刷新.
}
你可以用微软的工具看一下,使那些事件引起泄露
测试IE内存溢出工具测试下...什么没有溢出...就算是用了clearTimeout一样会.