【问题标题】:Javascript refreshing DIVJavascript 刷新 DIV
【发布时间】:2014-10-18 18:53:27
【问题描述】:

所以,我有这个脚本每 5 秒刷新一次我的 Div。在最初的几分钟左右,脚本运行良好,但 5-10 分钟后它不再刷新 div 或者刷新 Div 需要很长时间。我对javascript一无所知,所以有人可以告诉我问题出在哪里。该脚本用于刷新我的聊天室。代码如下:

<script type="text/javascript">
$(document).ready(function(){
  refreshTable();
});

function refreshTable(){
    $('#puu').load('<?php echo $kakka; ?>', function(){
       setTimeout(refreshTable, 5000);
    });
}
</script>

只是为了确保这里是整个代码:

<?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>

【问题讨论】:

  • &lt;?php echo $kakka; ?&gt; 这是网址吗?不要这么认为...

标签: javascript php jquery refresh chat


【解决方案1】:

我认为您需要将函数 setTimeout 更改为 setInterval。

 $(document).ready(function(){
   refreshTable();
 });

 function refreshTable(){
   $('#puu').load('<?php echo $kakka; ?>', function(){
   setInterval(function(){

     }, 5000);

   });
}

【讨论】:

    【解决方案2】:

    当您确认它工作了 2-3 分钟后,它的延迟似乎就像是内存泄漏。与刷新页面时相比,检查 5 分钟后浏览器是否消耗过多内存。另见jQuery load() method memory leak?

    正如链接中建议的那样,在.load() 调用之前尝试$.ajaxSetup({ cache: false });

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-14
      • 2020-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多