[英]Javascript refreshing DIV
So, I have this script refreshing my Div every 5 second. 因此,我有此脚本每5秒刷新一次Div。 For the first couple of minutes or so the script works just fine but after 5-10 minutes it no longer refreshes the div or it takes really long time for the Div to refresh.
在开始的前两分钟左右,脚本可以正常工作,但是在5-10分钟后,它不再刷新div,或者需要很长时间才能刷新div。 I understand nothing about javascript, so can somebody tell me what the problem is.
我对javascript一无所知,所以有人可以告诉我问题是什么。 The script is used to refresh my chatroom.
该脚本用于刷新我的聊天室。 Here is the code:
这是代码:
<script type="text/javascript">
$(document).ready(function(){
refreshTable();
});
function refreshTable(){
$('#puu').load('<?php echo $kakka; ?>', function(){
setTimeout(refreshTable, 5000);
});
}
</script>
Just to be sure here is the whole code: 只是为了确保这里是整个代码:
<?php
include '../dbcKIRJAUTUNUT.php';
page_protect();
error_reporting(0);
ini_set('display_errors', 0);
?>
<?php
$rs = mysql_query("SELECT * FROM users WHERE id='$_SESSION[user_id]'");
while
($row= mysql_fetch_array($rs))
{$starter= $row['id'];
$user_name= $row['user_name'];}
$starterID=$starter;
$companyID=$_GET['id'];
$input = $_POST['viesti'];
date_default_timezone_set('Europe/Helsinki');
$timestamp = date('h:i', time());
$file = $companyID."and".$starterID.".txt";
if (file_exists($file)) {
$kakka = $companyID."and".$starterID.".txt";
} else {
$kakka = $starterID."and".$companyID.".txt";
}
$current = file_get_contents($kakka);
if(isset($_POST['viesti']) && $_POST['viesti'] != null){
$shipuli= "<b>$user_name</b> <br> $input $timestamp\n<br>";
file_put_contents($kakka, $shipuli, FILE_APPEND | LOCK_EX);
}
echo '<div id="puu">'.$current.$shipuli.'</div>';
?>
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function(){
refreshTable();
});
function refreshTable(){
$('#puu').load('<?php echo $kakka; ?>', function(){
setTimeout(refreshTable, 5000);
});
}
</script>
<style>
#puu{
width: 300px;
height: 450px;
overflow: scroll;
}
</style>
</head>
<body>
<form method="post" id="norefesh">
<input type="text" name="viesti" autocomplete="off">
<input type="submit">
</form>
<form method="post">
<input type="submit" name="refresh" value="Päivitä">
</form>
</body>
</html>
I think that you need change your function setTimeout for setInterval. 我认为您需要为setInterval更改函数setTimeout。
$(document).ready(function(){
refreshTable();
});
function refreshTable(){
$('#puu').load('<?php echo $kakka; ?>', function(){
setInterval(function(){
}, 5000);
});
}
As you confirm it is working for 2-3 minutes and then its delayed seems like a memory leak. 当您确认它工作2-3分钟后,它的延迟似乎是内存泄漏。 Check if your browser is consuming too much memory after 5 minutes compared to what it was when you refreshed the page.
检查您的浏览器5分钟后是否比刷新页面时消耗了过多的内存。 Also see jQuery load() method memory leak?
另请参见jQuery load()方法内存泄漏?
As suggested in the link try $.ajaxSetup({ cache: false });
如链接中所建议,尝试
$.ajaxSetup({ cache: false });
just before .load()
call. 就在
.load()
调用之前。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.