Solved

jquery countdown - how to prevent timer from resetting on page reload?

Posted on 2011-09-24
3
3,793 Views
Last Modified: 2012-05-12
I'm using jquery countdown (http://keith-wood.name/countdown.html) in a script, but I can't figure out how to prevent the timer from resetting on a page reload...  Hoping someone knows how or can give me a suggestion?  I'm using php, so I thought maybe at some point, I can add a variable to a $_SESSION, but is that enough?
0
Comment
Question by:n00b0101
3 Comments
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
A full page reloads all code including javascript.  Any scripts running on the previous page are gone.  You would have to load a variable to restart the counter on the reloaded page.
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
There is no really good answer for what you want to do, but this might be somewhat useful.  First, read this article carefully so that you understand the DATETIME arithmetic issues.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

You can write a timer value into the browser output stream with something like the code shown in the snippet.  You can also keep a timer value in a data base or in the session array.  So every time the page is loaded, you can take the original time, subtract the time of "now" and that will give you the amount of remaining time to put into the JavaScript.  Obviously if the end of time has already come you will have some kind of special processing to do, like telling the client, "Sorry!"

HTH, ~Ray
<?php // RAY_countdown_timer.php
error_reporting(E_ALL);

// SET THE TIMER VALUE IN THE PHP SCRIPT
$timer = '7';
?>

<script type="text/javascript">
var interval = "";
var sekonds  = <?php echo $timer; ?>;

function startInterval()
{
    interval = window.setInterval("tTimer()",1000);
}
function stopInterval()
{
    window.clearInterval (interval);
    interval="";
}
function tTimer()
{
    document.thinkTime.tDisplay.value = sekonds--;
    if (sekonds == -1)
    {
        stopInterval();
    }
}
</script>

<script>
document.write('<meta http-equiv=\"Refresh\" content=\"<?php echo $timer; ?>;url=http://lmgtfy.com?q=JavaScript+Timer\">');
</script>

<form name="thinkTime">
This page will search for a JavaScript Timer in
<input readonly class="timerBox" size="1" type="text" name="tDisplay" value="">
<script>startInterval();</script>
seconds.
</form>
<a href="http://lmgtfy.com?q=JavaScript+Timer">Click here if you don't want to wait.</a>

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now