doushenxu7294 2016-04-13 22:38
浏览 125

有没有办法让页面无法刷新?

I have an HTML page that puts the worker on pause time which will be subtracted from his/her day job. The problem is that if he/she refreshes the time, the timer starts from the beginning automatically. I want it to continue in its normal flow. Is there any way to make the page unable to refresh? Some other kind of solution is also welcome. Thank you in advance.

<script>
$(".example").TimeCircles({start: true,circle_bg_color: "#FFFFFF", time: { Days: {show: false}, Hours: {show: false}, Minutes: {show: true}}})
.addListener(
function(unit,value,total) { 
    if (total == 20) { 
        document.body.style.backgroundColor = "#FFD150";

    } else if (total == 10) { 
   document.body.style.backgroundColor = "#E84141";

    } else if (total == 0) { 
   document.body.style.backgroundColor = "#000000";
    } 
} 

);

addEventListener("click", function() {
var
  el = document.documentElement
, rfs =
       el.requestFullScreen
    || el.webkitRequestFullScreen
    || el.mozRequestFullScreen
;
rfs.call(el);

});

</script>
  • 写回答

1条回答 默认 最新

  • dtot74529 2016-04-14 00:16
    关注

    Since you are using PHP, you can use the PHP $_SESSION variables. SESSION variables live on the server and are unique to the computer. You can also create a login system, and they can be unique to the logged-in user.

    Begin the page with:

    <?php
        session_start();
    

    before any other PHP code. I recommend placing it at the very top of the page, before the <DOCTYPE> declaration, before any PHP includes, before anything.

    That will give you access to the PHP $_SESSION variables, so you can do something like:

    <?php
        session_start();
        //Lots more PHP code in between
        $dt = date("Y-m-d H:i:s"); 
        $_SESSION['break_begin'] = $dt;
    

    The user can refresh the page till the cows come home, and the time will remain set. When they click a button, you can tell the page to create a "break_end" variable:

        $_SESSION['break_end'] = date("Y-m-d H:i:s");
    

    Later, you can subtract the two values and subtract the pause time from working time. (See refs below)

    Note that once the page is rendered, you cannot run any more PHP code. The way around this is called AJAX, and it's pretty straight-forward. Here is an SO answer that has information, and contains links to simple examples:

    Prevent Page Load on Jquery Form Submit with None Display Button

    In your case, you might need AJAX if you want to have two buttons on a web page: Begin Coffee Break and End Coffee Break. Clicking them will not run any PHP, and using javascript to store the time will create the problem you describe in your question. However, clicking a button can allow javascript to use AJAX... and AJAX can run a PHP file, setting (or reading) the PHP $_SESSION variables.

    Another thing to consider is that if you use localstorage or js cookies, a clever user could change the numbers on you. PHP variables live safely on the server. Only server-side code can change them.

    References that may be helpful:

    Subtracting two dates in php

    how to subtract two dates and times to get difference

    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100