How to Ajax auto refresh after x seconds
Update your contents after x seconds with ajax setInterval function
Realtime Listing, going very popular, every guy searching for that, i have also spend lot of time to search the way to refresh page every x seconds. so i can update my ajax listing with new added records.
after long search and try many method i found javascript function setInterval, all realtime listing works with it. i guess.. if not wrong
Use of setInterval function
just alert “hello San” to test this function
setInterval( "alert('Hello San')", 5000 ); ///////// 5 seconds
Example: just alert “hello San” to test this function after 10 seconds
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
<script language="JavaScript">
setInterval( "alert('Hello San')", 10000 );
</script>User it for call your main function.
setInterval( "SANAjax();", 10000 ); ///////// 10 seconds
Example : With jquery,update div contents after 5 seconds
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script> <script language="JavaScript"> setInterval( "SANAjax();", 5000 ); ///////// 10 seconds $(function() { SANAjax = function(){ $('#dataDisplay').prepend("Hi This is auto refresh example for you - w3cgallery.com <br><br>").fadeIn("slow"); } }); </script>
The code above will run every 10 seconds (10,000 ms)
i will call your function SANAjax();
Â
enjoy














September 5th, 2009 at 1:42 pm
thanks..ive been searching for the same thing..but a live example would really help
September 9th, 2009 at 8:27 am
Andrew, on your demand, demo is ready for you
December 13th, 2009 at 2:19 am
and the code?
December 14th, 2009 at 6:09 am
see above
January 21st, 2010 at 11:03 am
Nice code , Workign fine
April 19th, 2010 at 7:17 am
nice , quick & fast example ! all that I like
May 10th, 2010 at 8:17 pm
would it be possible to use this to refresh the content of a php page? if so how? i been trying to work this out for ages
May 11th, 2010 at 7:05 am
Yes its possible.
you need to do these. 1 make a function with ajax and connect with php pages to bring data and display in any div.
2nd add above function to refresh function. it will refresh and ajax function brings data from php page.
is it ok ?? if not let me know. i will write a post for it
May 11th, 2010 at 10:36 am
Hey SAN…
That would be fantastic if you could, also if you do, could you give me a buzz in my email provided.
Thanks guys,
Alec
May 11th, 2010 at 10:39 am
Hry, me again..
I would of thought that it would be something like this:
setInterval( “SANAjax();”, 5000 ); ///////// 10 seconds
$(function() {
SANAjax = function(){
$(’#dataDisplay’).html(”LOCATION OF PHP FILE”).fadeIn(”slow”);
}
});
Am I going in the right direction here?
Thanks guys, Alec
July 19th, 2010 at 5:57 am
what about add a checkbox to disable or enable those auto refresh ?
if unchecked it will be autorefresh, if checked it will stop the autorefresh ?
August 14th, 2010 at 11:14 pm
hey, i want to refresh just a part of the webpage, for example a chatbox - i wouldn’t want the whole page to be refreshing every 5sec but just the chatbox.. any ideas?
August 16th, 2010 at 7:36 am
you need to use div for chatbox, or use iframe, i guess so
August 31st, 2010 at 12:56 pm
Thank youuuuuuuuuuuuuu
Thought there was no hope till I found this post
=)
September 24th, 2010 at 11:25 am
worked for me………… thnx!!
October 8th, 2010 at 7:38 am
I put above code as usual. But no alert box is coming.
Please suggest how to put code.
thanks
October 25th, 2010 at 2:38 am
nice.
December 1st, 2010 at 6:26 am
Its not fetching the file content.
It just displaying the name of the file.
December 9th, 2010 at 12:34 pm
$(’#dataDisplay’).fadeOut(”slow”).load(’runninglist.php’).fadeIn(”slow”);
May 3rd, 2011 at 1:52 am
Hi,
how can i display updated data after saved in gridview using vb.net
thanks
June 9th, 2011 at 9:07 am
awsome and great
June 21st, 2011 at 10:22 am
Hi,
can i take this code to refresh an joomla modul?
many thanks
July 9th, 2011 at 3:39 pm
It works in firefox, but it doesn’t work in msie
http://www.ebar4u.com/user_details_guest.php?username=amsdamman&lang=dut
July 10th, 2011 at 12:48 pm
I was looking for how to use setInterval to call a function on my chat script. Thanks for this helpful guide
July 14th, 2011 at 3:23 am
Cool demo of your ajax file, big like!
July 20th, 2011 at 12:02 pm
Its so easy & quick thnks….
August 5th, 2011 at 10:40 am
does not works in IE.Destroys all the contents of page below specified div.Plz help
August 5th, 2011 at 10:41 am
Is there in special command for ie to run this script properly
August 18th, 2011 at 2:39 pm
please i need to know how to refresh tab only not all the window and how i use it in oracle ADF
thx
August 18th, 2011 at 5:07 pm
than you very much!
but I’ve problem!!
when I want to create a dynamic table by this.
the tag of mytable has row as it’s id
and the jsp tag () return somthing like this string value:
somthingsomthingsomthing
here is my codes:
selltb=function(){
$(’#rows’).text(”");
}
setInterval( “selltb();”, 1000 );
now it mast return me a dynamic table but… :(.
August 18th, 2011 at 9:53 pm
that’s funny!
when i change the table id from “table” to any thing dynamic rows will be create but other table functions (next page ,..) doesn’t!
what’s happened!!!
August 21st, 2011 at 1:36 am
I am trying to use this to refresh a php code. if i place the code into the div it works fine but when i do it through this it gives a fatal error but does refresh. how can i fix this? thank you!
August 21st, 2011 at 1:43 am
…Forgot to mention that i am doing it in wordpress
August 22nd, 2011 at 11:46 am
please provide us your codes or url to review that
September 2nd, 2011 at 3:30 pm
hello i am using <meta http-equiv=”refresh” content=”60″> at my blog and its working. thank you.
October 21st, 2011 at 7:40 pm
mthank’s for this share
November 21st, 2011 at 12:39 pm
hi,
i have used ur code. and it was awesome.
But when i have checked with crome and ie and safari then it does not work.
can you plz suggest me what to do??
Thank you.
regards,
ARCHIT
November 21st, 2011 at 2:25 pm
Archit, if you check “demo” link that works fine with IE,Chrome and safari too
December 14th, 2011 at 3:39 am
Did he ever post the code to have it read the php file?
December 20th, 2011 at 9:25 pm
hello,
Is it possible to use to auto refresh google adsence like in
http://www.musicindiaonline.com pls help
Regards
January 4th, 2012 at 1:25 am
Valuable information. Lucky me I found your site by accident, and I am stunned why this accident didn’t took place in advance! I bookmarked it.
January 17th, 2012 at 2:03 am
It’s in point of fact a great and useful piece of information. I am happy that you just shared this helpful info with us. Please keep us up to date like this. Thank you for sharing.