tags:

views:

34

answers:

3

Hey y'all.

Is this possible using jQuery/PHP? I am looking to have my webpage automatically go to a different web page at a certain time of day. For example, i have a 'breakfast' page that is supposed to auto-redirect to the 'Lunch' (www.mylunchpage.com) page at noon. Then the lunch page go to 'Dinner' (www.mydinnerpage.com) page at 5pm. I want it all based on time of day on not on a timer.

Yes, the browser will be open all day...

Any idea on how to make this work? Any advise will be greatly appreciated.

+2  A: 

Use setInterval to check the time once every minute. Then use new Date().getHours() to get the current hour. If the hour has changed to the one you want, go to the next page.

You dont need jQuery for this so far. If you want server time instead of client time, you'd have to start by getting the time difference from the server on startup via ajax.

James Westgate
A: 

The Javascript function setInterval() can be used to check at certain intervals (every minute, every 5 minutes, etc.) to see what time it is. If the current time hits one of your targets, you redirect.

Something like:

function checkTime()
{
  var date = new Date();
  var hour = date.getHours();
  var min = date.getMinutes();

  if (hour == 12 && min == 0)
    window.location = 'noon.html';
  else if (hour == 17 && min == 0)
    window.location == '5pm.html';
}

$(document).ready(function(){
  setInterval(checkTime, 60000); // This will run checkTime every minute
});
ABach
I think that will only run once. I would call checkTime from document.ready, then move your setTimeout code to the bottom of the checkTime function. It is also better to use a function instead of a string as a parameter to setTimeout/setInterval.
James Westgate
@James: Nope. This runs fine. The note about using a function rather than a string is dead-on, though.
Jon Purdy
@Jon: Its been changed to setInterval from setTimeout now. EDIT: Now its been changed again, but not back to setTimeout like it should if being called from the same function.
James Westgate
Wow, that is exactly what I had in mind ABach. Thanks a ton.James W, you do bring up a good point, i hadnt even thought of server time. I need to brainstorm pros and cons there. Yes, ABachs code will only work once for that page, but the redirect pages will have the same code, so i think it will flow seamlessly.Thank you both.
Rhepungus
Okay, sorry for the confusion - this version should work.
ABach
Oh i see. Thanks again: ABach, Westgate, Purdy.
Rhepungus
This is what happens when people make edits too quickly. O_o
Jon Purdy
Haha, understood @Jon - I think we managed to get out unscathed. ;)
ABach
A: 

setInterval is a good strategy. I only have to add that navigating to another page is pretty annoying to user. If it is all about the style of the page: better just change CSS dynamically. Will grant better user experience. You can even play with colors and emulate "dusk" and "dawn" changing colors smoothly from red to dark blue.

Drop a comment if you need the example of how to change CSS from JS at the page.

Juriy
This is actually a really good point. Unless there is tons of content that needs to change between these various pages, a CSS stylesheet change may be the better route.
ABach
Juriy, the content will change dramatically. but i do like the idea of changing css dynamically. Does it use the same idea listed below?
Rhepungus