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

ajax, jquery, refresh

Solution

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

Problem

I've been trying to do this for a lot of time, but I can't. Here is my problem: I have a web page just like this: ``` <html> <body> <div id="my_div"> <span id="txt">HELLO WORLD</span> </div> </body> </html> ``` Now I want that the div "my_div" refreshes itself every X seconds, also refreshing its content. How to do this using AJAX or JQUERY or JAVASCRIPT? Pay attention: I don't require a script that refreshes the entire page. Pay attention(2): I've just tried to find something by Google, but I found nothing. Please, help me. I have been having this problem for many time. Thank you in advance!

Original source