My problem is this causes more lag as time goes on. I use this commonly to chat with my friends, and I need it the site to update in real-time whenever a message gets edited/deleted/added. Any solutions?
function update(){
var i = 0;
var leadsRef = firebase.database().ref(room);
leadsRef.on('value', function(snapshot) {
var old_room = room;
snapshot.forEach(function(childSnapshot) {
var childData = childSnapshot.val();
if(i === 0){
document.getElementById("comments").innerHTML = "";
}
document.getElementById("comments").innerHTML += encode(childData.datee);
document.getElementById("comments").innerHTML += " ";
document.getElementById("comments").innerHTML += encode(childData.namee);
document.getElementById("comments").innerHTML += ": ";
document.getElementById("comments").innerHTML += encode(childData.contentss);
document.getElementById("comments").innerHTML += "<br>";
i += 1
}); i = 0; return 0;
});
}
window.setInterval(update, 100);
The full code can be found here.
+=and constant reloading of all messages, you need to find a way to track what the "last" message was and only load "new" messages, that will be the first step towards success. \$\endgroup\$