oTree Forum >

How to disable countdown timer reset on page refresh?

#1 by Daniel,

Hello all,

I wrote the following script for a countdown timer, however, it will be reset on page refresh. Do you have any idea of how to disable timer reset or disable the browser refresh button. Thank you very much in advance! :)

Best Regards,

var saved_countdown = localStorage.getItem('saved_countdown');
var sstseconds = 1;

// Update the countdown every 1 second
function() {

  // Get today's date and time
  var now = new Date().getTime();
  // Find the distance between now and the countdown date
  var distance = countDownDate - now;
// Time calculations for days, hours and seconds
if(distance >= 0) {
window.sstseconds = Math.floor((distance % (1000 * 60)) / 1000);
document.getElementById("demo1").style.color = '#2ecc71'; 
document.getElementById("demo1").innerHTML = window.sstseconds + "s ";
if(distance < 0) {
window.sstseconds = Math.ceil((distance % (1000 * 60)) / 1000);
document.getElementById("demo1").style.color = 'red';
document.getElementById("demo1").innerHTML =  "Overtime! " + window.sstseconds + "s";
, 1000);


#2 by szhangjake,

Hi Daniel,

  I have a rough idea but have not yet put it into code: we can store the time a player leaves the previous page, and pass this timestamp via js_vars or vars_for_template into this template. So that every time a player refreshes the page, we could compare the current time with the time of departure from the last page. And we pass the time difference as an argument of the timer like what you have done above.
 I think this part on [Timeouts that span multiple pages](https://otree.readthedocs.io/en/latest/timeouts.html#timeouts-that-span-multiple-pages) is relevant. Hope it helps.

#3 by Daniel,

Hello Jake,

Thank you very much for your reply! This is a brilliant idea. It is very helpful. :)


#4 by shanys,

Hi both,

linked to what Jake suggested, that's how I coded it under the relevant Page class:

    def vars_for_template(player: Player):
        if not player.time_decision:
            player.time_decision = -time.time()

    def before_next_page(player: Player, timeout_happened):
        player.time_decision = round(player.time_decision + time.time(), 2)
  Of course feel free to round to the decimal you prefer.

Write a reply

Set forum username