[ACCEPTED]-How to refresh <DIV>-html
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() {
$('#recent_activity').load('recent_activity_ajax.php');
}, seconds*1000)
}
function cancelActivityRefresh() {
clearInterval(timer);
}
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() {
startActivityRefresh();
});
If this 16 is not at all what you meant by refreshing 15 the <div>
, you're going to need to clarify. :)
EDIT:
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...
You can write a function something like 1 this and call it on 'document.ready'
function refreshMyDiv() { $("myDiv").fadeOut("slow"); //Code to refresh the content goes here //could be a AJAX call $("myDiv").fadeIn("slow"); setTimeout("refreshMyDiv();", 60000); }
http://www.ajtrichards.co.uk/heartbeat/
$(document).ready(function(){
$.jheartbeat.set({
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.
}); });
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() {
$('#recent_activity').load('recent_activity.php');
}, 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
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.