Solved

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

Posted on 2011-09-24
3
3,931 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 83

Expert Comment

by:Dave Baldwin
ID: 36593833
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 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 36593863
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
ID: 36593913
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

776 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