Javascript 在运行时检索注释?
我想调用Ajax,它从数据库中获取所有注释 现在,当只有人评论时,如何检查要运行的Ajax脚本 与堆栈溢出通知相同。当我们对问题进行评论时,通知会在不重新加载页面的情况下出现(即在运行时) 现在,我在每10秒钟一次又一次地运行同一个Ajax脚本,我认为这是一种不好的方式Javascript 在运行时检索注释?,javascript,php,jquery,mysql,ajax,Javascript,Php,Jquery,Mysql,Ajax,我想调用Ajax,它从数据库中获取所有注释 现在,当只有人评论时,如何检查要运行的Ajax脚本 与堆栈溢出通知相同。当我们对问题进行评论时,通知会在不重新加载页面的情况下出现(即在运行时) 现在,我在每10秒钟一次又一次地运行同一个Ajax脚本,我认为这是一种不好的方式 $(document).ready(function(){ setInterval(function(){ $.ajax({ type: "GET", ur
$(document).ready(function(){
setInterval(function(){
$.ajax({
type: "GET",
url: "ajax_files/reload_notifications.php"
}).done(function(result) {
var $notifications = $('#notification_area');
if ($notifications.length > 0) {
$notifications.html(result);
}
});
}, 10000);
});
输入注释后,执行类似的AJAX请求以保存注释,并在成功回调时调用另一个函数,该函数也将命中另一个获取注释的AJAX请求。例如
function insertComment() {
$.ajax({
type: "GET",
url: "ajax_files/insert_comment.php"
}).done(function(result) {
fetchComments();
}
});
}
function fetchComments() {
$.ajax({
type: "GET",
url: "ajax_files/reload_notifications.php"
}).done(function(result) {
var $notifications = $('#notification_area');
if ($notifications.length > 0) {
$notifications.html(result);
}
});
}
希望这有帮助 使用HTML5服务器发送的事件better@JasonOOO你能给我举个例子吗?或者帮助我在代码中使用它。我会非常感激的。使用.html()的.append()instaed。试试Google@GurminderSingh不,亲爱的,这不是问题所在。我想知道注释何时到达数据库,所以同时只有我的脚本应该运行