Current location: Hot Scripts Forums » General Web Coding » JavaScript » periodic javascript refresh

periodic javascript refresh

Reply
  #1 (permalink)  
Old
mattdc2001's Avatar
Newbie Coder
 
Join Date: Jun 2007
Location: North Carolina
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
periodic javascript refresh

I have a piece of code that will let me refresh the page on the hour and 30 minutes into the hour:
javascript Code:
  1. <SCRIPT LANGUAGE="JavaScript" type="text/javascript">
  2. function startTimer() {
  3.     var now = new Date();
  4.     var minutes = now.getMinutes();
  5.     var seconds = now.getSeconds();
  6.     setTimeout('refresh()',
  7.     (((30 - (minutes % 30) - ((seconds>0)?1:0)) * 60    ) + (60 - seconds)) * 1000);}
  8. function refresh() {
  9.     window.location.href = 'word.php';
  10. }
  11.  
  12. startTimer();
  13. </SCRIPT>

I would like to change the code so it will refresh the page on the hour, 15 minutes in, 30 minutes in, and 45 minutes in. Basically 4 times an hour I need the page to refresh. I just don't know exactly how to do it. I didn't write this code, and I didn't know where to begin.
__________________
Andrei, you've lost another submarine?
Reply With Quote

Featured Marketplace Scripts (view more)

  #2 (permalink)  
Old
UnrealEd's Avatar
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 27 Times in 27 Posts
if you just want it to refresh the page every 15 minutes (4 times/hour), you don't need to get the current date (unless you want it to be exactly at 0mins, 15mins, 30mins and 45mins:
javascript Code:
  1. function startTimer() {
  2.   // 15*60*1000 = 15 mins, the time-param is in milliseconds
  3.     setTimeout('refresh()',15*60*1000);}
  4. function refresh() {
  5.     window.location.href = 'word.php';
  6. }

if you want it to refresh at the exact times, i think this should do:
Javascript Code:
  1. function startTimer() {
  2.     var now = new Date();
  3.     var minutes = now.getMinutes();
  4.     var seconds = now.getSeconds();
  5.     var secTime = minutes*60*seconds;
  6.     // if the rest of the division is zero, this means that the time is either 0, 15, 30 or 45 mins
  7.     if(secTime % (15*60) == 0){
  8.       var refreshTime = 15*60*1000;
  9.     // else calculate the difference in time till the next lap:
  10.     } else {
  11.       var refreshTime = (secTime % (15*60))  * 1000;
  12.     }
  13.     setTimeout('refresh()', refreshTime;}
  14. function refresh() {
  15.     window.location.href = 'word.php';
  16. }
  17.  
  18. startTimer();
the code isn't tested, but i think it should work. Just give it a shot
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks


Last edited by UnrealEd; 06-19-07 at 10:57 AM.
Reply With Quote
  #3 (permalink)  
Old
mattdc2001's Avatar
Newbie Coder
 
Join Date: Jun 2007
Location: North Carolina
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
I tested the timed version, but it didn't quite work right. First I had to change the ";" to a ")" at the end of the line":
javascript Code:
  1. setTimeout('refresh()', refreshTime;}

I opened the browser about 50 minutes after the hour and it refreshed at 55 minutes after the hour.
__________________
Andrei, you've lost another submarine?
Reply With Quote
  #4 (permalink)  
Old
mattdc2001's Avatar
Newbie Coder
 
Join Date: Jun 2007
Location: North Carolina
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
I think I figured it out. I replaced:
javascript Code:
  1. else {
  2.       var refreshTime = (secTime % (15*60))  * 1000;
  3.     }
with:
javascript Code:
  1. else {
  2.       var refreshTime = (secTime - (secTime % (15*60))) * 15 * 1000;
  3.     }

I'm giving it a trial run for the next hour.
__________________
Andrei, you've lost another submarine?
Reply With Quote
  #5 (permalink)  
Old
UnrealEd's Avatar
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 27 Times in 27 Posts
that can't be right: if secTime=2760 (46mins), the refresh should be in 14 minutes. But when you test it with your code, you will get:
2760 - 60 * 15 * 1000 = 40 500 000

i think what you need is this:
javascript Code:
  1. var refreshTime = (15*60 - (secTime % (15*60))) * 15 * 1000;

sorry about the syntax error, i wrote it on the spot, so ....
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
  #6 (permalink)  
Old
mattdc2001's Avatar
Newbie Coder
 
Join Date: Jun 2007
Location: North Carolina
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
javascript Code:
  1. var refreshTime = (15*60 - (secTime % (15*60))) * 1000;
was the final piece of code that works like a charm. That extra 15 was prolly left over from my last failed attempt. But all is good.

Thanks a lot guys!
__________________
Andrei, you've lost another submarine?
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Add javascript after load? <?Wille?> JavaScript 14 03-31-06 04:52 AM
JavaScript command to refresh IFrame Content Thomas Walker JavaScript 2 05-05-05 10:06 AM
Order of vbscript and javascript in ASP marlin ASP 0 06-03-04 03:01 PM
javascript refresh and show only after refresh completed bonefry JavaScript 7 06-03-04 04:25 AM
Reaaly stuck about javascript over frames muratisik JavaScript 1 12-14-03 11:58 AM


All times are GMT -5. The time now is 12:00 AM.
vBulletin® Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.