我希望能够使用jQuery AJAX从数据库中刷新或获取更新的记录,而不用刷新页面。
现在,我只是在页面加载时加载记录,但我希望能够在页面已加载并且已发布评论时自动加载记录。
我只想要一个简单的解决方案,不必要求适用生产,因为这只是我正在研究的一个学校项目。
我想每20秒就有一个Ajax请求,或者在用户发表评论时调用更新功能。
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>
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.
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.