简体   繁体   English

单击按钮打开模式时禁用 location.reload()

[英]Disable location.reload() when click button to open modal

I have a page that reloads to update information in a database.我有一个页面可以重新加载以更新数据库中的信息。 THe page will show a list of files with some information about each file.该页面将显示一个文件列表,其中包含有关每个文件的一些信息。 To show more detailed info about the file the user can click a button for that file and open a modal with more information.要显示有关文件的更多详细信息,用户可以单击该文件的按钮并打开包含更多信息的模式。

My problem is that the reload function kicks in and closes the modal.我的问题是重新加载 function 启动并关闭模式。 The update interval needs to be 10 seconds but when the modal is open I would either disable the reload or change the time value to a few minutes instead.更新间隔需要为 10 秒,但是当模式打开时,我会禁用重新加载或将时间值更改为几分钟。 And after the modal is closed the timer should go back to 10 seconds.并且在模态关闭后,计时器应该 go 回到 10 秒。

           <script type="text/javascript">
         setTimeout(function(){
                location.reload();
                },5000)
        </script>

        <script type="text/javascript">
        $(document).ready(function(){
        $('.orderinfo').click(function(){
        var orderId = $(this).data('id');
        // AJAX request
        $.ajax({
        url: 'ajaxfile.php',
        type: 'post',
        data: {orderNo: orderId},
            success: function(response){ 
                // Add response in Modal body
                $('.modal-body').html(response);
                // Display Modal
                $('#empModal').modal('show'); 
                }
                });
            });
        });
        </script>

And in the HTML part this opens the modal.在 HTML 部分中,这将打开模态。

echo "<td style='width:15%'><button data-id='".$orderNo."' class='orderinfo'>Read report</button></td>";

I have tried different things but can't find a way to fix it.我尝试了不同的方法,但找不到解决方法。 Appreciate any help感谢任何帮助

Thanks谢谢

you can use clearTimeout() to stop.您可以使用 clearTimeout() 停止。

     var t = 0;

    function timer_pause(){
        clearTimeout(t);
        console.log('pause');
    }

    function timer_start(){
        t = setTimeout(function(){
                    location.reload();
                    },5000);
    }


    $(document).ready(function(){
        timer_start();
        $('.orderinfo').click(function(){
            timer_pause();
       });
    // use #modalid or whatever modal selector you have 
    // or if you have close function put resume in it
      $('.myModal').on('hidden.bs.modal', function () {
         timer_start();
      })

   })

this way when you modal open it will pause the timer and when modal is close it will reset it, you can make adjustment to continue the timer with previous time,这样,当您打开模式时,它会暂停计时器,当模式关闭时,它将重置它,您可以进行调整以继续使用之前的时间,

I wont suggest you reload the page too often, you can just reload the content, if you are showing a table, use datatables, or just simple ajax.我不建议您经常重新加载页面,您可以重新加载内容,如果您正在显示表格,使用数据表,或者只是简单的 ajax。

Thank you Ahmed Sunny.谢谢艾哈迈德桑尼。

I almost got it working.我几乎让它工作了。 It open the modal and stops the timer.它打开模态并停止计时器。 So far so good.到目前为止,一切都很好。

But I don't get it to start the timer when closing the modal.但是我在关闭模式时没有启动计时器。 Here is how the code looks now.这是代码现在的样子。

<script>
        var t = 0;
        function timer_pause(){
            clearTimeout(t);
            console.log('pause');
        }

        function timer_start(){
            t = setTimeout(function(){
            location.reload();
            },5000);
        }

        $(document).ready(function(){
            timer_start();
            $('.orderinfo').click(function(){
                timer_pause();
                var orderId = $(this).data('id');
                // AJAX request
                $.ajax({
                url: 'ajaxfile.php',
                type: 'post',
                data: {orderNo: orderId},
                    success: function(response){ 
                        // Add response in Modal body
                        $('.modal-body').html(response);
                        // Display Modal
                        $('#empModal').modal('show'); 
                    }
                });

            // use #modalid or whatever modal selector you have 
            // or if you have close function put resume in it
            $('.orderInfo').on('hidden.bs.modal', function () {
                timer_start();
                })
            });                   
        })
        </script>

In the HTML for the close button I have this:在 HTML 的关闭按钮中,我有这个:

<div class="modal-footer">
<button type="button" class="button" data-dismiss="modal">Close</button>
</div>

I guess I'm not have the connection right here.我想我这里没有连接。

You have to excuse me I haven't done anything with this kind of stuff before.你得原谅我以前没有用这种东西做过任何事情。 Just plain PHP, HTML and CSS只是普通的 PHP、HTML 和 CSS

Thanks谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM