Rectangle 27 1

javascript Set countdowntimer to continue after reset or do not reset after refreshing the page?


$.ajax({url: "somePath/updateTimeLeft.php?timeLeft=1234"});
<?php
  if(isset($_GET['timeLeft'])){
    $_SESSION['timeLeft'] = $_GET['timeLeft'];
  }
?>
if(isset($_SESSION['timeLeft'])){
  echo 'var timeLeft = ' . $_SESSION['timeLeft'] . ';';
  $_SESSION['timeLeft'] = undefined;
}else{
  echo 'var timeLeft = 0;';
}

The only way I can think of setting the $_SESSION variable of time left, would be to use an ajax call inside your javascript every second or so, that calls a php script with a get variable in the filepath. This php script would set a session variable, and your timer page would check if something like $_SESSION['timeLeft'] was set, and echo out a js variable for timeleft, if not set echo out 0.

and when you load the page, in your php

Note
Rectangle 27 1

javascript Set countdowntimer to continue after reset or do not reset after refreshing the page?


<div class="countdown"></div>
<button style="display:none">FINISH!!!</button>
<script>
    $(document).ready(function() {
        var timer2 = localStorage.getItem('timer');
        if(timer2 === null) timer2 = "1:30";
        $('.countdown').html(timer2);

        var interval = setInterval(function() {
            var timer = timer2.split(':');
            var minutes = parseInt(timer[0], 10);
            var seconds = parseInt(timer[1], 10);
            --seconds;
            minutes = (seconds < 0) ? --minutes : minutes;
            if (minutes < 0){
                clearInterval(interval);
                localStorage.removeItem('timer');
                $('button').show();
            }else{
                seconds = (seconds < 0) ? 59 : seconds;
                seconds = (seconds < 10) ? '0' + seconds : seconds;
                $('.countdown').html(minutes + ':' + seconds);
                timer2 = minutes + ':' + seconds;
                localStorage.setItem('timer',timer2);
            }
        }, 1000);
    });
</script>

Just use the browser Window.localStorage (or sessionStorage if it fits better).

Thanks It worked. it continues on refreshing the page...

Note