简体   繁体   English

如何使用javascript每1秒在html网站上打印.txt文件内容?

[英]How to print a .txt file content in a html site with javascript every 1 s?

I have a file in my directory which is constantly changing itself. 我的目录中有一个不断变化的文件。 So every 1 second my file is saved and has a diffenent content. 因此,我的文件每1秒保存一次,并且内容不同。 How can I display the file content in a HTML site using javascript in a way that the file content is updated in real time or every 1 s? 如何使用javascript以实时或每1秒更新文件内容的方式在HTML网站中显示文件内容?

I tried this code but the site just opens the pop-up window where the file content is and every time I want to see changes, I have to press F5, I don't want that. 我尝试了这段代码,但网站只是打开文件内容所在的弹出窗口,每次我想看到更改时,我都要按F5,我不希望这样。

<html>
<head>

</head>
<title>test</title>
<body>
<a href="output.txt">test</a><br>

    <script>
        var w = window.open('output.txt'); //Required full file path.
        w.print();
    </script>
</body>
</html>

I would go along the lines of 我会顺其自然

</head>
<title>test</title>
<body>
<a href="output.txt">test</a><br>
    <div id="text" ></div>
    <script>
        setInterval(()=>{
               let t=new XMLHttpRequest()
               t.open('GET',location.origin+'output.txt')
               t.onreadystatechange=(ev)=>{
                   if(ev.readyState==4){    
                       document.getELementById('text').textContent=t.responseText;
                      }
                };
                t.send()
         },1000);
    </script>
</body>
</html>

this way you wont get a new popup every second another way would be : 这样你每秒钟都不会得到一个新的弹出窗口:

<html>
<head>

</head>
<title>test</title>
<body>
<br>
    <iframe id="text" src="output.txt" />
    <script>

        setInterval(()=>{ 
            document.getElementById("text").contentWindow.location.reload();
         },1000);
    </script>
</body>
</html>

Use the following code: 使用以下代码:

 <html> <head> </head> <title>test</title> <body> <a href="output.txt">test</a><br> <script> var w = window.open('output.txt'); //Required full file path. w.print(); setInterval(function() { window.location.reload(true); }, 1000) </script> </body> </html> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM