Julian
Julian

Reputation: 657

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.

Upvotes: 1

Views: 1135

Answers (4)

Peeyush
Peeyush

Reputation: 4828

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

Upvotes: 0

Parixit
Parixit

Reputation: 3855

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.

Upvotes: 1

Kevin G Flynn
Kevin G Flynn

Reputation: 231

Please try below code :

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

        });
  }, 6000);

Your div reload after 6 second.

Upvotes: 0

Dan
Dan

Reputation: 3103

You can try this:

setTimeout(function() { 
    document.title = "new title";
    $(".divname").text("new div text");
}, timeout);

Upvotes: 0

Related Questions