[ACCEPTED]-How to refresh <DIV>-html

Accepted answer
Score: 16

Based on x0n's comment, which I think is 31 a fair guess, I am guessing that you want 30 to automatically refresh a <div> with content 29 from the server every X seconds. In your 28 example, your second argument to the setInterval function 27 is 1. This is a really bad idea, as the 26 delay is in milliseconds, so you would be 25 firing off 1000 requests a second (not that 24 the browser would let you, but still).

Anyhow, if 23 that's what you want, you would need to 22 do this:

var timer;
var seconds = 30; // how often should we refresh the DIV?

function startActivityRefresh() {
    timer = setInterval(function() {
    }, seconds*1000)

function cancelActivityRefresh() {

Your server-side script (recent_activity_ajax.php in the 21 example) would then need to return whatever 20 you want the <div> to be populated with - just 19 that, not the headers or footers or anything 18 else. If you wanted this to start when the 17 page loads, you would simply call startActivityRefresh:

$(function() {

If this 16 is not at all what you meant by refreshing 15 the <div>, you're going to need to clarify. :)


In 14 response to your comment: you can't make 13 Javascript "refresh" the contents of the 12 dynamic PHP code. This just isn't possible 11 as the browser can't really execute a server 10 side language like PHP. You have to call 9 the server again. In order to make this 8 cleanest you should probably move the code 7 that fills the contents of #recent_activity to a function, call 6 that once when the page loads and have a 5 different file that simply outputs that 4 code to be able to refresh it dynamically. You 3 should probably look into MVC patterns from 2 here, but I'm not sure if you're quite ready 1 for that...

Score: 1

You can write a function something like 1 this and call it on 'document.ready'

function refreshMyDiv()
   //Code to refresh the content goes here
   //could be a AJAX call
   setTimeout("refreshMyDiv();", 60000);

Score: 1


   url: "data.php", // The URL that jHeartbeat will retrieve
   delay: 1500, // How often jHeartbeat should retrieve the URL
   div_id: "test_div" // Where the data will be appended.
}); });


Score: 0

there is nothing about the div 'recent_activity' that 8 perpetually synchronizes with your server. However, supposing 7 you had some dynamic webpage recent_activity.php 6 which returns current data from your recent-activity 5 database, you could load that into the div 4 every once in a while with

var refresh = setInterval(function() { 
}, 1);

P.S. 1 millisecond 3 spamming is usually a bad idea. Look into 2 the comet pattern if you want sub-second 1 responses to activity.

More Related questions