How to use AJAX for auto refresh a div or a web page How to use AJAX for auto refresh a div or a web page ajax ajax

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


you can write some stuff like

var doSth = function () {  var $md = $("#my_div");  // Do something here};setInterval(doSth, 1000);//1000 is miliseconds


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.