我的问题是,有什么更好的方式来做评论系统就像在StackOverflow, 我的意思是我发送请求从我的浏览器,每个人都会看到这个评论(或在其他浏览器)没有刷新页面像一些聊天。阿贾克斯发表评论喜欢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>');
}
});
}
[WebSockets的](https://developer.mozilla.org/en/docs/WebSockets)比的setInterval一个更好的解决方案。 –
在每个'#send'上添加'setInterval'而不清除前面的''''听起来像是一个危险的想法。 'var tot = parseInt(max - cnt);'而不是简单的'var tot = max - cnt;'看起来很奇怪。 'parseInt(tot)<= 0'而不是'tot <= 0'也很奇怪。 – Regent
如果您熟悉.Net技术,请尝试在SignalR中为您提供良好的解决方案。 –