0

I am trying to make a real time comment system.I have already made this by using jquery setTimeout. But problem is every interval of time page is refreshing, so I think this is not a good way.

Here is the code

 function update_comment(){
        setTimeout(function(){
            $.getJSON("comment_fetch.php",function(data){
                $.each(data.result,function(){
                    //console.log(this["pid"]);
                    $(".commentItem"+this["pid"]).append("<p class='message'><a class='name' href='#'><small class='text-muted pull-right'><i class='fa fa-clock-o'></i>"+this["dc"]+"</small><i class='fa fa-user'></i>"+this["un"]+"</a>"+this["c"]+"<a href='#' class='replyLink'><i class='fa fa-pencil'></i> Reply</a></p>");
                });
                $.each(data.pidcc,function(){
                    //console.log(this["pid"]+" "+this["pcc"]);
                    $("#cmnt_count"+this["pid"]).html("Comment ("+this["pcc"]+")");
                });
            });
            update_comment();
        },200);
    }

have there any solution to make a real time comment system without using setTimeout ?

Alimon Karim
  • 4,354
  • 10
  • 43
  • 68

1 Answers1

0

Of course the page is refreshing, you're adding more data to it. What specifically are you trying to avoid?

In Javascript, you have basically two methods of code execution: timers, or events. (and technically, a timer is an event, too, but it's handled differently) It's probably not likely that you want to load things based on a user or browser event, so you either need some kind of external system to notify you when updates happen (perhaps via WebSocket), or you need to do it via a timer.

Eric Blade
  • 161
  • 7