2017-06-22 36 views
1

我正在我的网站的论坛页面上,并试图包括编辑和删除发布的登录用户谁发起帖子,如下所示的功能:已删除的帖子与AJAX仍然显示在其他用户页面,直到刷新

function getPosts(){ 
     $.getJSON("fetch_post.php", function(data){ 
      $.each(data, function(i, user){ 
       if (user.parent_id == 0){//if its a post 
        if ($('.media-wrapper .media[data-id = '+user.id+']').length == 0){//if post does not exist on the page before 
         var userName = $(".gist_params input.uname").val(); 
         if (userName == user.name){//if logged in user is the originator of this post 
          var post= //an html showing the post, and then a delete and edit button 
          }else{//if its another user's post 
           var post= //an html showing only the post, without delete and edit button     
          } 
         $(".media-wrapper").prepend(post); 
        } 
       }else{//if its a reply to a post 
        $.each(data, function(i, user){ 
        if ($('.reply_media .reply_media_body[data-id = '+user.id+']').length == 0){ 
         var par_id = user.parent_id; 
         var parent_cont = $('.media-wrapper .media[data-id = '+par_id+']'); 
         var reply = //an html showing the reply to the post 
         parent_cont.find('.media-body .reply_media').prepend(reply); 
         } 
        }); 
       } 
      }); 
     }); 
    } 
    getPosts(); 
    setInterval(getPosts, 5000); 

这里是我删除邮编:

$(document).on("click", ".media .comm_del", function(e){ 
     e.preventDefault(); 
     if (confirm("Are you sure you want to delete the post?")){ 
      var post_id = $(this).attr('del-id'); 
      var post_to_del = $('.media-wrapper').find('.media[data-id = '+post_id+']'); 
      $.post("del_post.php", {"post_id": post_id}, function(data){ 
       if (data = "yes"){ 
        post_to_del.remove(); 
        getPosts(); 
       } 
      }); 
     } 
    }); 

注:请注意,发布工作正常,所有用户的页面都会自动更新。问题出在删除帖子时,只有删除的用户看到了他/她的Feed中的更新,而其他用户仍然看到删除的帖子,直到他们的页面被刷新。我想要的是所有用户页面在setInterval到期时自动更新。

回答

1

因此,如果您之前没有看到过新帖子,但是没有任何会删除已不在列表中的旧帖子。

更新之前,获取页面上所有当前帖子ID的列表。在您浏览列表时,请注意您看到的ID。当你完成,删除任何不是看到。

function getPosts() { 
    var existing = {}; 

    $('.media-wrapper .media[data-id]').each(
    function() { 
     var did = this.getAttribute('data-id'); 

     existing[did] = false; 
    } 
); 

    $.getJSON("fetch_post.php", function(data) { 
    $.each(data, function(i, user) { 
     existing[user.id] = true; 

     if (user.parent_id == 0) { //if its a post 
     if ($('.media-wrapper .media[data-id = ' + user.id + ']').length == 0) { //if post does not exist on the page before 
      var userName = $(".gist_params input.uname").val(); 
      if (userName == user.name) { //if logged in user is the originator of this post 
      var post = //an html showing the post, and then a delete and edit button 
      } else { //if its another user's post 
      var post = //an html showing only the post, without delete and edit button     
      } 
      $(".media-wrapper").prepend(post); 
     } 
     } 
     else { //if its a reply to a post 
     $.each(data, function(i, user) { 
      if ($('.reply_media .reply_media_body[data-id = ' + user.id + ']').length == 0) { 
      var par_id = user.parent_id; 
      var parent_cont = $('.media-wrapper .media[data-id = ' + par_id + ']'); 
      var reply = //an html showing the reply to the post 
       parent_cont.find('.media-body .reply_media').prepend(reply); 
      } 
     }); 
     } 

     for (var did in existing) 
     { 
     if (! existing[did]) 
     { 
      $('.media-wrapper .media[data-id=' + did + ']').remove(); 
     } 
     } 
    }); 
    }); 
} 
相关问题