Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-09-24
3
Medium Priority
?
4,449 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 84

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 111

Accepted Solution

by:
Ray Paseur earned 2000 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

636 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