所以,我所做的就是从文本文件中收集信息,然后将其转换为 html 页面上的文本。代码如下:
HTML:
<div id="text" class="text"></div>
JavaScript:
var text_print = new XMLHttpRequest();
text_print.open("GET", "file.txt", true);
text_print.onload = function (){
document.getElementById("text").innerHTML=text_print.responseText;
text_print.send(null);
我现在需要做的是让这个脚本每秒运行一次。
我的服务器中的file.txt
不断变化,所以我需要当文件变化时屏幕上打印的信息也随之变化。
谢谢。
最佳答案
这不是最有效的方法,但如果您仍然决定继续,这可以帮助您。
window.setInterval(function(){
var text_print = new XMLHttpRequest();
text_print.open("GET", "http://localhost/post/file.txt", true);
text_print.send();
text_print.onreadystatechange = function () {
if (text_print.readyState == 4) {
if(text_print.status == 200) {
document.getElementById("text").innerHTML=text_print.responseText;
}
}
}
}, 1000);
关于javascript - 创建一个循环来读取文本文件或实时更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43876218/