这个想法是创建一个 html 滚动条来实时接收日志。我正在使用FLASK,它对于创建网页非常有用,但到目前为止只知道处理该工具的网页的基础知识。
莫回无
浏览 128回答 2
2回答
繁华开满天机
获取一些文字将其填充到元素中将元素滚动到底部// don't need this. It's to get the posts from the fake APIlet id = 0;// get a reference to the logs elementconst logs = document.getElementById("logs");// this takes some text, wraps in in a <pre> tag and returns the elementfunction makeLogEntry(str) { const el = document.createElement("pre"); el.innerText = str; return el;}// This is just to tick up the post id and roll it back to 1 if we go over 100, since the fake api only has 100 postsfunction getId() { return ++id > 100 ? 1 : id;}function getPost(id) { fetch(`https://jsonplaceholder.typicode.com/posts/${id}`) .then(response => response.json()) .then(json => { // wrap the log message in a tag and append it to the logs logs.append(makeLogEntry(json.title)); // scroll the logs element to the bottom // see (https://stackoverflow.com/questions/270612/scroll-to-bottom-of-div) logs.scrollTop = logs.scrollHeight; })}// fetch stuff every 2 secondssetInterval(() => getPost(getId()), 2000);#logs { width: 100%; height: 100px; border: 1px solid #ddd; overflow: scroll;}<div id="logs"></div>