JSSuicide
JSSuicide

Reputation: 45

Countdown timer from a start date and time

Hi all I have the following countdown timer in JS - that I use across pages:

<script type="text/javascript">
     $(function() {
         var count = localStorage.getItem('count') || 60
         countdown = setInterval(function() {
             localStorage.setItem('count'), count);
             $("span.countdown").html(minutes + ":" + seconds + " Remaining");

             if (count == 0) {
                   clearInterval(countdown);
                   localStorage.removeItem('count');
             }
             count--;
         }, 1000);
    });
</script>

Im finding that if the user makes many PHP post requests on my page the timer doesn't really countdown.

I was wondering whether it is possible to store a date time in localstorage and then when the user reloads the page - calculate the difference between the time in local storage and the time now - many apologies for the lack of code I am a complete JS noob

Upvotes: 0

Views: 549

Answers (2)

Basti
Basti

Reputation: 130

You could use the Date object to get the current time: http://www.w3schools.com/js/js_obj_date.asp

Examples:

new Date().getTime() // gets the amount of alle milliseconds since 1970 => also usefull for the serialization

new Date(milliseconds) // restoring the date object


I hope, that helps you

Upvotes: 0

JayEdgar
JayEdgar

Reputation: 196

In PHP, set a session variable, $_SESSION['EndTime']. As each page loads, compare the current time with session variable and output the remaining time.

Upvotes: 0

Related Questions