php - 如果引导模式处于事件状态或正在显示,如何停止每秒刷新一次的页面?

标签 php jquery mysql ajax bootstrap-4

我有两个 php 页面,第一页是我使用 Ajax 显示来自 MySQL 数据库的数据,每 1 秒刷新一次,第二页包含从数据库获取数据的 php 代码,其中我有一个引导模式显示每条记录的其他信息。

第一个 php 页面:

<div class = "container">
   <div id = "show_MySql_table_data"></div>
</div>

<script>
//Ajax script for displaying the record every 1 second
 var interval = 1000;
 function displayData() {
   $.ajax ({
    url: "details_info.php",
    type: "POST",
    data: {},
    dataType: "html",
    success: function(data) {
        $("#show_MySql_table_data").html(data);
    },
    complete: function (data) {
        setTimeout(displayData, interval);
    }       
   });
  }
   setTimeout(displayData, interval);
  </script>

第二个 php 页面:

<?php
$cn = mysqli_connect("localhost","root","","testdb");
$sql = "SELECT * FROM tblsample";
$result = mysqli_query($cn,$sql);
if (mysqli_num_rows($result) > 0 ) {
    while($rows = mysqli_fetch_array($result)) {
        $name = $rows['Name'];
        $address = $rows['Address'];
        echo "<tr>";
        echo "<td>" . $name . "</td>";
        echo "<td>" . $address . "</td>";
        echo "<td><button class = 'toggleDetailsModal btn btn-info'>More Info</button></td>";
        echo "</tr>";
    }
}
?>

<!-- Modal -->
<div class = "modal" id = "detailsModal">
<div class = "modal-dialog">
    <div class = "modal-content">
        <div class = "modal-header">
            <h5 class = "modal-title">Details</h5>
            <button class = "close" data-dismiss = "modal">&times;</button>
        </div>
        <div class = "modal-body">
            More Information here....
        </div>
        <div class = "modal-footer">
            <button class = "btn btn-danger btn-sm" data-dismiss = "modal">Close</button>
        </div>
    </div>
</div>

<script> //this is included in the second php page
$(".toggleDetailsModal").on("click", function(e) {
   e.preventDefault();
   $("#detailsModal").modal('show');
});
</script>

发生的情况是,当我单击“更多信息”按钮时,模式会自动关闭,并且我的屏幕会变灰。

当我单击“更多信息”按钮并且模态框处于事件状态或处于显示模式时,如何停止自动刷新页面(每 1 秒)?

提前谢谢您。

最佳答案

有多种方法可以确定模式是打开还是关闭,一种选择是 Hook Bootstrap 模式的事件:

https://getbootstrap.com/docs/4.0/components/modal/#events

show.bs.modal

This event fires immediately when the show instance method is called. If caused by a click, the clicked element is available as the relatedTarget property of the event.

hide.bs.modal

This event is fired immediately when the hide instance method has been called.

添加到现有代码:

var modalActive = false;
$("#detailsModal").on("show.bs.modal", function() {
    modalActive = true;
});
$("#detailsModal").on("hide.bs.modal", function() {
    modalActive = false;
});

function displayData() {
    if (!modalActive) {
        $.ajax ...
    } else {
        setTimeout(displayData, interval);
    }
}

关于php - 如果引导模式处于事件状态或正在显示,如何停止每秒刷新一次的页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55915430/

相关文章:

php - php中变量与数组的速度

javascript - 使用 jquery 向同一页面发送 GET 请求

javascript - 在输入焦点上打开 Windows 10 触摸键盘的数字版本

mysql - Laravel 通知基表未找到

mysql - 有什么办法可以优化这个存储过程吗?

php - While 循环显示不同行而不是同一行

javascript - 为我的 children 制作一个数学游戏;如何根据选择切换图像?

jquery - window.getSelection().getRangeAt() 无法正常工作

MySQL 查询 - 使用 URL 名称识别数据,其中数据被组织成层次结构

php - 将数组变量导出到 csv 时,如何删除带有 2 个或多个变量的双引号,这些变量形成一个字符串?