【发布时间】:2013-12-01 16:03:08
【问题描述】:
我正在使用 AJAX、PHP 和 MySQL 开发一个网络聊天网站。
但是我遇到了一个问题,有一个功能可以检查与您聊天的人是否仍在聊天(在线)或他/她已放弃聊天。 函数如下所示:
//some variables here
var somedata="....";
var userleftchatmsg="he/she left you,you are alone now.";
function checkifuseronline(){
$.ajax({
type: "GET",
url: "check.php",
dataType: "json",
data:somedata
}).done(function(r) {
if (r.o1==2) {
$("#chattable").prepend(userleftchatmsg);
} else {
checkifuseronline();
}
});
}
问题是“他/她离开了..”消息有时会出现两次,尤其是当用户同时与多个伙伴聊天时。 这不应该是不可能的还是我错了?
【问题讨论】:
-
什么是
r?也许更新不够快? -
您将消息添加到
#chattable元素,所以我不明白为什么它不能显示两次。如果您拨打checkifuseronline两次并获得r.o1 == 2两次,您将看到两次消息。 -
也许如果你使用 setTimeout 函数再次调用 checkifuseronline 就像
setTimeout('checkifuseronline()', 1000); -
你什么时候调用 checkifuseronline()
-
这个函数会每秒用http调用轰炸你的服务器,直到用户离开?