1

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.

Julian
  • 657
  • 3
  • 9
  • 32
  • 1
    Whats your main logic behind refreshing the div are you trying to refresh to load some data inside div ? – Veerendra Nov 20 '13 at 04:56
  • Hello @Veerendra, The Logic behind it is say like a presenter is on air for example "John Smith - 09:00" when the next presenter is on air at 12:00 I wish the div to change to "Peter Smith - 12:00" without the listener having to refresh. – Julian Nov 25 '13 at 09:31

4 Answers4

1

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.

Parixit
  • 3,829
  • 3
  • 37
  • 61
  • Is it possible to make it refresh the div with information from a list based on what time it is? So say like I have a list of people and each person had a time that they are on air. I would need the script to select a person based on what time it is and display it in the div. Do you know what I mean? Is it possible? – Julian Nov 25 '13 at 09:40
  • @user3011524 you mean to get user's system time and work according to it. – Parixit Nov 28 '13 at 16:30
  • Yes that's exactly what I mean :) – Julian Nov 28 '13 at 20:28
  • @Julian Please go with this question: http://href=%27http://stackoverflow.com/a/863624/631652 – Parixit Nov 29 '13 at 07:33
0

You can try this:

setTimeout(function() { 
    document.title = "new title";
    $(".divname").text("new div text");
}, timeout);
Dănuț Mihai Florian
  • 3,075
  • 3
  • 26
  • 44
  • Thank you for you answer, this is similar to what I am after but I wish to refresh at a certain time for example 12:00pm then refresh again at 15:0pm. The script must be able to get the time from the listeners computer so it know exactly when to refresh. – Julian Nov 25 '13 at 09:35
0

You can take a look at jQuery's load method.

By using is you can load the content of you div.

    setTimeout(function() { 
      $("#youfirstdivid").load("url1"); 
       $("#youseconddivid").load("url2");

      $('title').text("new title"); 
   }, timeout);

Don't forgot to wrap your code inside

$(function(){
  //you code goes here

});

and if you want to refresh it at some fixed interval then you can use setInterval rather than setTimeout

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Peeyush
  • 4,728
  • 16
  • 64
  • 92
  • Thank you for your answer but this isn't quite what I am after. I need to refresh those divs at certain times like 4pm then 8pm for example. – Julian Nov 25 '13 at 09:40
0

Please try below code :

setTimeout(function(){
        $('#divid').load("pageurl #divid" >*",function(){

        });
  }, 6000);

Your div reload after 6 second.

Kevin G Flynn
  • 231
  • 4
  • 14
  • Thank you for your answer but this is not what I am after. – Julian Nov 25 '13 at 09:32
  • This is exactly what I'm looking to do and mine too is for a radio station https://stackoverflow.com/questions/73784917/ajax-update-of-div-at-specific-hour-of-the-day I have a time interval as per the answers here but not yet worked out how to do it at a specific time eg. 2pm, 5pm. – jfar_2020 Sep 20 '22 at 22:52
  • I know you posted this 9 years ago but... if I solve this I'll update you! – jfar_2020 Sep 20 '22 at 22:54