How to Ajax auto refresh after x seconds


19 Votes, Rating: 4.211 Star2 Stars3 Stars4 Stars5 Stars

Tags: , ,

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 icon biggrin How to Ajax auto refresh after x seconds

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

Share/Save/Bookmark

42 Responses to “How to Ajax auto refresh after x seconds”

  1. Andrew says:

    thanks..ive been searching for the same thing..but a live example would really help :)

  2. SAN says:

    Andrew, on your demand, demo is ready for you

  3. kzok says:

    and the code?

  4. SAN says:

    see above

  5. James says:

    Nice code , Workign fine

  6. Dax says:

    nice , quick & fast example ! all that I like :)

  7. ANdrew says:

    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

  8. SAN says:

    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

  9. Alec says:

    Hey SAN…

    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

    That would be fantastic if you could, also if you do, could you give me a buzz in my email provided.

    Thanks guys,
    Alec

  10. Alec says:

    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

  11. Dedy says:

    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 ?

  12. Marc says:

    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?

  13. SAN says:

    you need to use div for chatbox, or use iframe, i guess so

  14. Xee.O says:

    Thank youuuuuuuuuuuuuu
    Thought there was no hope till I found this post
    =)

  15. afreen says:

    worked for me………… thnx!!

  16. rakesh says:

    I put above code as usual. But no alert box is coming.
    Please suggest how to put code.
    thanks

  17. baganaa says:

    nice.

  18. Akram says:

    Its not fetching the file content.

    It just displaying the name of the file.

  19. Sahir says:

    $(’#dataDisplay’).fadeOut(”slow”).load(’runninglist.php’).fadeIn(”slow”);

  20. seyda says:

    Hi,
    how can i display updated data after saved in gridview using vb.net
    thanks

  21. aykak says:

    awsome and great

  22. Nicole says:

    Hi,
    can i take this code to refresh an joomla modul?

    many thanks

  23. hans says:

    It works in firefox, but it doesn’t work in msie :(

    http://www.ebar4u.com/user_details_guest.php?username=amsdamman&lang=dut

  24. Pilot says:

    I was looking for how to use setInterval to call a function on my chat script. Thanks for this helpful guide :)

  25. Referencement sites says:

    Cool demo of your ajax file, big like!

  26. Rizwan khan says:

    Its so easy & quick thnks….

  27. sonal says:

    does not works in IE.Destroys all the contents of page below specified div.Plz help

  28. sonal says:

    Is there in special command for ie to run this script properly

  29. Mahmoud Helaly says:

    please i need to know how to refresh tab only not all the window and how i use it in oracle ADF

    thx

  30. Sasan says:

    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… :(.

  31. Sasan says:

    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!!!

  32. DarkArtist69 says:

    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!

  33. DarkArtist69 says:

    …Forgot to mention that i am doing it in wordpress

  34. SAN says:

    please provide us your codes or url to review that

  35. ?JAKIR! says:

    hello i am using <meta http-equiv=”refresh” content=”60″> at my blog and its working. thank you.

  36. mode says:

    mthank’s for this share

  37. archit says:

    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

  38. SAN says:

    Archit, if you check “demo” link that works fine with IE,Chrome and safari too

  39. Justin B says:

    Did he ever post the code to have it read the php file?

  40. Arif says:

    hello,
    Is it possible to use to auto refresh google adsence like in
    http://www.musicindiaonline.com pls help

    Regards

  41. référencement naturel says:

    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.

  42. New Language Better Than PHP says:

    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.

Leave a Reply