0

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>
timolawl
  • 5,434
  • 13
  • 29
Henri Sula
  • 41
  • 6
  • what do u mean its impossible lol ? – Henri Sula Apr 13 '16 at 22:41
  • 2
    You can update the backend with the timer info. SO if they refresh the page the info will be stored already and the timer won't refresh back to 0. – odannyc Apr 13 '16 at 22:41
  • `window.onunload=function(){localStorage.old=counter;}; ... counter=+localStorage.old || 3600;` – dandavis Apr 13 '16 at 22:44
  • it IS possible to disable refresh and nag on navigating away, but not to stop tab closes... – dandavis Apr 13 '16 at 22:45
  • @Dandavis nag on it, sure, but you shouldn't be able to disable refresh _ever_ (and if you can't you shouldn't do it). It's a browser navigation function- something that should be sacred. – abluejelly Apr 13 '16 at 22:54
  • somehow you can, i have a bug to fix where it takes two F5 presses to reload, no onbeforeunload in play... – dandavis Apr 14 '16 at 00:21

1 Answers1

1

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

Community
  • 1
  • 1
cssyphus
  • 37,875
  • 18
  • 96
  • 111