There already is an answer for autoscrolling, but that has a problem. If the user has manually scrolled it up to read old logs, that code keeps auto-scrolling, interfering the user's reading. So, I want it to auto-scroll only when it is showing the last line (i.e., either the user has never scrolled it up, or scrolled it up and then scrolled down to the bottom). How can I determine that?
var output;
var i = 0;
function onLoad() {
output = document.getElementById("output");
onTimeout();
}
function onTimeout() {
i++;
var line = document.createElement("div");
line.innerText = "Log " + i;
output.appendChild(line);
var isShowingTheLastLine = true;
if (isShowingTheLastLine) {
output.scrollTop = output.scrollHeight;
}
setTimeout(onTimeout, 1000);
}
<body onload="onLoad()">
<div id="output" style="overflow-y:scroll; height:200px; width:300px; background:yellow"></div>
</body>