javascript - 刷新 div 时 while 循环被终止

标签 javascript php jquery

我有这段 JavaScript 代码每 5 秒刷新我的 watcher.php 文件,效果很好。第一个页面加载正常,但 5 秒后,while 循环被终止。有什么理由吗?

index.php 文件

<script type="text/javascript">
  var auto_refresh = setInterval(function(){
    $('.view').load('watcher.php');
  }, 5000); // refresh div time
</script>

<body>
  <div class="view">
    <?php include 'watcher.php'; ?>
  </div>
</body>
...

watcher.php 文件

include 'config.php';

$sql = "SELECT id, name, available, will_return, status  FROM check_in_out";
$result = $conn->query($sql);

echo '<div style="margin-top:35px;"><center>';

echo '<table class="pure-table">
<thead>
<tr>
    <th>Name</th>
    <th>Available</th>
    <th>Status/Comments</th>
    <th>I\'ll Return At:</th>
    <th>Returned</th>
</thead>
</tr>
';

if ($result->num_rows > 0) {
    $i = 1;
    // output data of each row 
    while ($row = $result->fetch_assoc()) {
        echo '<tr>';
        echo '<td><img style="vertical-align:middle" src="imgs/card.jpg"> <a class="various" href="#'.$i.'"/>'.$row["name"];'</a></td>';
        echo "<td>"; 
            if ($row['available'] == 1) {
                echo "<img src='imgs/available.gif'/>";
                echo "Online";
            } else {
                echo "<img src='imgs/not-available.gif'/>";
                echo "<span style='vertical-align:middle'>Away</span>";
            };
        echo "</td>";
        echo '<td>'.$row["status"];'</td>';
        echo '<td>'.$row["will_return"];'</td>';
        echo '<td></td>';
        echo '</tr>';
        echo '<div align="center" id="'.$i++.'" style="display:none;width:520px;">
            <h2>'.$row["name"].'</h2>
            <!-- <h4>Current Status: '.$row["status"].'</h4> -->

            <form method="post" action="statusChange.php" />
            <input type="hidden" value="'.$row["id"].'" name="id"/>

            <textarea rows="4" cols="50" placeholder="Enter Comment/Status Here..." name="comment"></textarea>
            <br/><br/>
            When will you return? - <input type="time" name="e_time">
            <br/><br/>
            <input class="pure-button pure-button-primary" type="submit" value="Leave/Return"> 
            </form> 
        </div>';
       }
} else { 
    echo "0 employees";
}

echo '</tr></table>';
echo '</div></center>';

$conn->close();




更新:

这是什么,请看下图。您会注意到,在第一次加载(5 秒)后,可点击的(href)被杀死......

enter image description here

最佳答案

<script type="text/javascript">
var updateWatcher = function() {

   $.ajax({
      url: 'watcher.php',
      success: function(response) {

         $('.view').html(response);

         window.setTimeout(function(){
             updateWatcher();
         }, 3500);
      }
   });

}

updateWatcher();
</script>

当然,这个问题可以通过 promises 之类的东西来更优雅地解决。 ,但就简单的修复而言,您可以尝试类似的方法,其中仅在成功接收到响应后才设置计时器。这个应该比较靠谱。

这是一个简单的 fiddle :http://jsfiddle.net/agentfitz/26mahomm/3/ (查看控制台)。

关于javascript - 刷新 div 时 while 循环被终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33812194/

相关文章:

javascript - 如何使用排序键中的任何值查询 DynamoDB?

PHP kerberos 委派

php - Kubernetes Ingress NGINX for Laravel 通过 fast-cgi

c# - "Upload"使用jQuery上传文件不点击

javascript - 从 php 返回的 json 无法解析为 jQuery dataTables

javascript - 制作一个链接,点击后消失并显示一个 div

javascript在获取新内容时对高度进行动画处理

javascript - AJAX/拒绝身份验证的 HTTP header ?

php - 获取特定分类术语的高级自定义字段组

javascript - 如何让 jquery select2 动态禁用一个选项?