我到这行了,我真的很努力地执行离线部分。
当用户在线时显示一次。没关系。但是,当用户脱机时,控制台中每10秒就会显示--- is offline
。我只想让它说is offline
,当用户脱机然后停下来直到它再次联机。请在这里帮助我。
window.setInterval(function() {
if (document.getElementById('statusIndexer') == null)
return;
for (var i = 0; i < WLAPStore.Presence.models.length; i++) {
var m = WLAPStore.Presence.models[i];
var id = 'p' + m.id.slice(0, -5);
var clientBox = document.getElementById(id);
if (clientBox !== null) {
var img = clientBox.getElementsByTagName('img')[0];
img.classList.remove('isOnline');
if (m.isOnline) {
console.log(id + ' is online');
clientBox.parentNode.prepend(clientBox);
img.classList.remove('isOffline');
img.classList.add('isOnline');
} else {
console.log(id + ' is offline');
clientBox.parentNode.prepend(clientBox);
img.classList.remove('isOnline');
img.classList.add('isOffline');
(document.getElementById('statusIndexer') == null)
return;
}
}
}
}, 1000);
答案 0 :(得分:1)
使用全局变量保留以前的状态,并在记录消息之前检查它是否已更改。
var onlineState = {};
window.setInterval(function() {
if (document.getElementById('statusIndexer') == null)
return;
for (var i = 0; i < WLAPStore.Presence.models.length; i++) {
var m = WLAPStore.Presence.models[i];
var id = 'p' + m.id.slice(0, -5);
var clientBox = document.getElementById(id);
if (clientBox !== null) {
var img = clientBox.getElementsByTagName('img')[0];
img.classList.remove('isOnline');
if (m.isOnline) {
if (m.isOnline !== onlineState[id]) {
console.log(id + ' is online');
}
clientBox.parentNode.prepend(clientBox);
img.classList.remove('isOffline');
img.classList.add('isOnline');
} else {
if (m.isOnline !== onlineState[id]) {
console.log(id + ' is offline');
}
clientBox.parentNode.prepend(clientBox);
img.classList.remove('isOnline');
img.classList.add('isOffline');
(document.getElementById('statusIndexer') == null)
return;
}
onlineState[id] = m.isOnline;
}
}
}, 1000);