SupaOden
SupaOden

Reputation: 742

Ajax request after page load

I want to be able to refresh or get newer records from the database using jQuery AJAX. without refreshing the page.

Here is the trick, since someone will post a comment, how may I call an Ajax request. Since that comment can be posted anytime.

Right now I am just loading the records on page load. I want to be able to load the records when the page has been already loaded and a comment has been posted.

I just want a simple solution, this project is not for production. Its just a school project that I am working on.

I was think of a Ajax request every 20s or perhaps call an update function when a user comments.

Upvotes: 2

Views: 2454

Answers (3)

SupaOden
SupaOden

Reputation: 742

var auto_refresh_comments = setInterval(
function () {
$('#comments').load('reload_comments.php?meeting='+ meeting_id+'').fadeIn("slow");
}, 5000); // refresh every 5000 milliseconds

Reloads the #comment element after every 5 seconds.

Upvotes: 0

Mithun Satheesh
Mithun Satheesh

Reputation: 27835

do it like this.

<script language="javascript">

//function that refresh the comment list
function load_new_comments() {
    //jquery ajax call to pull the new records and update the comment area
}

//function to add a comment into dataase
function add_new_comment() {
    //jquery ajax call to add the new records 
   load_new_comments();
}

//callback function.refresh time set to 30 seconds.
function callback() {
    setTimeout("pollingFunction();",30000);
}

//refresh function
function pollingFunction() {
    load_new_comments();
    callback();
}

$(document).ready(function() {
     pollingFunction();//initiating the function for the 1st time.
 });

</script>

Upvotes: 1

LostMohican
LostMohican

Reputation: 3144

its easy, do the posting comments with javascript, after each javascript request to server, do a page refresh request to get the newer comments and posts, distribute them accordingly in your markup, then again you can use setInterval for doing this operation on a second basis.

Upvotes: 0

Related Questions