How to use AJAX for auto refresh a div or a web page
Using jQuery load()
and an interval timer is about the simplest.
setInterval(function(){ $('#my_div').load('/path/to/server/source');}, 2000) /* time in milliseconds (ie 2 seconds)*/
load()
is a shorthand method for $.ajax
This assumes that you would set up a server side script that only outputs the content for that element.You could also use a selector fragment within load
url to parse a full page for the specific content
See load() API Docs
Using jQuery, you can dynamically update the content of an element using:
$("#my_div").html("... new content ...");
The new content replaces the original content.
Using the setInterval() method, you can cause JavaScript to be executed every period:
For example:
window.setInterval(function() { $("#my_div").html("... new content ...");}, 1000);
Would replace the content every second.