<分区>
我的问题是,像 StackOverflow 这样的评论系统有什么更好的方法, 我的意思是我从我的浏览器发送请求,每个人都会看到这个评论(或在其他浏览器中),而无需像聊天一样刷新页面。
我的解决方案是使用setInterval
,但我认为必须有另一种方式
$(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>');
}
});
}