Connecting Tech Pros Worldwide Forums | Help | Site Map

Remaining time from db with no page refresh

Newbie
 
Join Date: Aug 2007
Posts: 23
#1: Jun 25 '08
hi in my php page i am showing to the user the remaining time untill an event occurs..
I am reading the value from a database
The problem is that he needs to refresh the page to get the correct time (for time to decrement) ...i can use a meta refresh but it doesn't look professiona, is there a way for the values to decrement without page refresh and if yes how? (can you please provide a code snip-set)
The only way that i can think maybe is by using an ajax function that queries the db every now and then but i am not sure how to call it as you normally associate it with events (like onclick, onscroll) or how to associate a timer.. but this might be the wrong thought ..

any help really appreciated!

Thanks

Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,752
#2: Jun 25 '08

re: Remaining time from db with no page refresh


Hi.

You could always just have Javascript count down from a given time and update your page.

Somewhat like:
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. // Initialize the Date object.
  3. //  The set methods should be filled in by PHP
  4. var _date = new Date();
  5. _date.setHours(<?php echo $hour; ?>);
  6. _date.setMinutes(<?php echo $min; ?>);
  7. _date.setSeconds(<?php echo $sec; ?>);
  8.  
  9. // Generates a HH:MM:SS string
  10. function parseDate(dateObj) {
  11.   var output = "";
  12.  
  13.   if(dateObj.getHours() < 10) {
  14.       output += "0";
  15.   }
  16.   output += dateObj.getHours() + ":";
  17.  
  18.   if(dateObj.getMinutes() < 10) {
  19.    output += "0";
  20.   }
  21.   output += dateObj.getMinutes() + ":";
  22.  
  23.   if(dateObj.getSeconds() < 10) {
  24.    output += "0";
  25.   }
  26.   output += dateObj.getSeconds();
  27.  
  28.   return output;
  29. }
  30.  
  31. // Start the countdown
  32. setInterval(function() {
  33.     _date.setSeconds(_date.getSeconds() - 1);
  34.     document.getElementById('Display').innerHTML = parseDate(_date);
  35. }, 1000);
  36. </script>
  37.  
  38. <span id="Display">Loading...</span>
  39.  
Needs Regular Fix
 
Join Date: Mar 2008
Posts: 311
#3: Jun 25 '08

re: Remaining time from db with no page refresh


The previous suggestion using a countdown timer written in Javascript on the client side is a good suggestion.

But if you still for some reason need to read values from the database (or for that matter periodically call any PHP script in the background) then you can associate a function that does this background call, for example using Ajax or, as I do it, by submitting a hidden iframe on the page, with a Javascript countdown timer.

In words:

1) On page load, a javascript function is called which initiates a timer.
2) When the timer has counted down to zero, a function is called which does your background tasks (database query, updating fields or values on the current page) using Ajax or by just submitting a hidden iframe.
3) The javascript function which initiates the timer is then called again.

If, for example, you set the timer to 30 seconds, then your background query will be carried out every 30 seconds and then the timer will start counting again.
Reply


Similar PHP bytes