Julian Julian - 7 months ago 31
Javascript Question

Refresh Div at a certain time

Ok so I am creating a radio player and basically I need the Title, Content div, Next show Div to refresh at certain times for example 9am then 12pm. I have the JQuery code to refresh the page at a certain time but that isn't quite what I'm after. Any ideas?

Code:

function refreshAt(hours, minutes, seconds) {
var now = new Date();
var then = new Date();

if(now.getHours() > hours ||
(now.getHours() == hours && now.getMinutes() > minutes) ||
now.getHours() == hours && now.getMinutes() == minutes && now.getSeconds() >= seconds) {
then.setDate(now.getDate() + 1);
}
then.setHours(hours);
then.setMinutes(minutes);
then.setSeconds(seconds);

var timeout = (then.getTime() - now.getTime());
setTimeout(function() { window.location.reload(true); }, timeout);
}


Then I just call the refreshAt function by inserting the following on my page

<script type="text/javascript">refreshAt(04,30,0);</script> //page refreshes at 4:30am.


So this refreshes the Whole page. I just need to refresh the Title, and 2 divs.
What do I need to add/change in the code.

Answer

This will call your current URL after interval elapse and reset your title and your div content.

You have to write something like this:

function refreshAt(hours, minutes, seconds) {
   var now = new Date();
   var then = new Date();

   if(now.getHours() > hours || (now.getHours() == hours && now.getMinutes() > minutes) || now.getHours() == hours && now.getMinutes() == minutes && now.getSeconds() >=  seconds) {
           then.setDate(now.getDate() + 1);
   }
   then.setHours(hours);
   then.setMinutes(minutes);
   then.setSeconds(seconds);

   var timeout = (then.getTime() - now.getTime());
   setTimeout(function() {
      $.get( window.location.pathname, function( data ) {
             $('title') = $(data).filter('title').text();
             $('.DIV_CLASS') = $(data).filter('.DIV_CLASS').html();
             alert( "Load was performed." );
     });
   }, timeout);
}

NOTE: This script use jQuery, so please include latest jQuery library.