How to Ajax auto refresh after x seconds


11 Votes, Rating: 4.551 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 :D

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

14 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
    =)

Leave a Reply


CSS (Design) - TOP.ORG