My question is, what is the best way to make a comment system, for example, on StackOverflow, I mean, I send a request from my browser, and everyone will see this comment (or in another browser) without refreshing the page, for example, in chat.
My solution was to use setInterval , but I think there should be a different way
$(document).ready(function() { get(); $('#send').click(function() { $.post('http://localhost/mvc.com/comment/post', { n_id: parseInt(newsId), user_id: $('#uid').val(), text: $('#content').val(), token: $('#token').val() }, function (ret) { if (ret.comm.err) { $('.f').empty().prepend('<li id=e><h3 style="color: red">ERROR</h3></li>'); return false; } get(); }); setInterval(get,3000); }); $('#content').keypress(function(e){ var key = e.which; var cnt=$(this).val().length; var max=100; var tot=parseInt(max-cnt); if(key >= 33 || key == 13 || key == 32) { if (parseInt(tot) <= 0) { e.preventDefault(); } } }); function get() { $.post('http://localhost/mvc.com/comment', {get: parseInt(newsId)}, function (ret) { $('.f').empty(); for (var key in ret.comm) { $('.f').append('<li class=c id=' + ret.comm[key].id + '><span>' + ret.comm[key].name + '</span><hr><br>' + ret.comm[key].text + '</li>'); } }); }
source share