你好我已經開發了使用socket.io,expressjs和貓鼬它的工作正常的聊天應用程序。它會在幾秒鐘後刷新並從db中提取新的客戶端(如果存在)。問題在於用戶可以感覺到div是正在刷新的,並且還有一段時間需要時間來回應。如何避免這種情況。這裏是我的代碼 這是我的服務器端代碼刷新內容不刷新頁面
setInterval(function() {
var allOnLine;
allOnLine = io.sockets.clients();
for (var client in allOnLine) {
if (allOnLine[client].username == "undefined") {
continue;
} else {
notifyAll(allOnLine[client].username);
}
}
}, 50000);
,這裏是通知所有方法
function notifyAll(userName) {
contactModel.find({'userId':userName}, (function (err, contactModel) {
usernames = [];
var contacts = contactModel;
for (var a = 0; a < contacts.length; a++) {
usernames[a] = contacts[a].contactId;
}
var allOnLine;
allOnLine = io.sockets.clients();
for (var client in allOnLine) {
if (allOnLine[client].username == "undefined") {
continue;
} else {
for (var i = 0; i < usernames.length; i++) {
if (allOnLine[client].username == usernames[i]) {
usernames[i] = usernames[i] + " -On";
}
allOnLine[client].username);
}
}
}
io.sockets.to(userName).emit('updateusers', usernames);
}));
}
這是我的客戶端代碼
socket.on('updateusers', function(usernames) {
jQuery('#usernames').html('');
jQuery.each(usernames, function(key, value) {
jQuery('#usernames').append('<div class="chatContact" id="chatLink" onclick="privateChat(\''+value+'\')">');
}}
任何幫助我也發佈了這個問題,但沒有回答
可能[刷新內容不刷新頁面](http://stackoverflow.com/questions/14338544/refresh-the-content-without-page-refresh) –