【问题标题】:Retrieve comments on runtime?检索运行时的评论?
【发布时间】:2013-09-26 17:58:39
【问题描述】:

我想调用 Ajax 从数据库中获取所有 cmets。

现在如何检查要运行的 Ajax 脚本,当只有某人 cmets 时。

stackoverflow 通知 相同。当我们评论问题时,通知会出现而不重新加载页面(即在运行时)。

现在我每 10 秒一次又一次地运行相同的 Ajax 脚本,当我认为这是一种不好的方式时。所以这是我的工作 Ajax 代码:

$(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);
});

【问题讨论】:

  • 使用 HTML5 服务器发送事件,更好
  • @JasonOOO 你能给我举个例子吗?或者帮助我的代码使用它。我会很感激的。
  • 使用 .append() 代替 .html()
  • @GurminderSingh 不,亲爱的,这不是问题所在。我想知道评论什么时候出现在数据库上,所以同时只有我的脚本应该运行

标签: javascript php jquery mysql ajax


【解决方案1】:

当输入评论时,执行类似的 AJAX 请求来保存评论,并在其成功回调上调用另一个函数,该函数也将触发另一个获取 cmets 的 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);

        }
   });
 }

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2015-08-03
    • 2012-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-06
    • 1970-01-01
    • 1970-01-01
    • 2011-03-21
    相关资源
    最近更新 更多