473,322 Members | 1,431 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Remaining time from db with no page refresh

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
Jun 24 '08 #1
2 2848
Atli
5,058 Expert 4TB
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.  
Jun 25 '08 #2
coolsti
310 100+
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.
Jun 25 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: JGH | last post by:
I want to create a web page that allows a user to run an oracle stored procedure. That's easy enough but the problem is that the procedure might take several seconds to complete. I don't want...
14
by: edykstra | last post by:
Hello, If you point your browser to this page, http://prdownloads.sourceforge.net/vnc-tight/tightvnc-1.2.9_x86_viewer.zip?use_mirror=umn the server sends you the HTML page, and very soon...
4
by: hupjack | last post by:
I finally joined the millions of cell phone users out there. I'm the 2nd phone on what is now a family share plan. (Our two cell phones use minutes from a central 400 minute peak time pool.)...
9
by: PK9 | last post by:
I'm having an issue with the "Refresh" of an asp.net page. The refresh is actually calling my last onClick event. I thought that asp.net was supposed to be stateless in that it shouldn't...
2
by: SJ | last post by:
I have a request to the server that could take upto 10 mins to come back and display the results. I want to either a) Redirect to a new "Please wait" page till the processing is done, then...
3
by: flashvarss | last post by:
Hi, I am making a web-based chat but to get the msg from the database i need to refresh the page and that will make load and extra bandwidth on the server so i want to make the query in real time...
11
by: tdmailbox | last post by:
I have a website that I subscribe to that allows me to track tasks. I have an extra computer with a big monitor that i want to always display the task list. the problem is after so many hours I...
3
by: Fuzzy | last post by:
I've been using a javascript window system to make windows appear within a project of mine. within these dragable windows i have iframes which seems a great way to split up into forms, and much more...
1
by: panos100m | last post by:
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.