javascript 在网页上显示每秒更新的 txt 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6450921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 20:42:55  来源:igfitidea点击:

Show a txt file on a webpage which updates every second

javascriptjqueryhtmlajax

提问by lemon

I'm sort of shooting in the dark here; I have no knowledge how to do this so some pointers and/or links to helpful tutorials would be great:

我有点像在黑暗中射击;我不知道如何做到这一点,所以一些指向有用教程的指针和/或链接会很棒:

I have a website that I want to display a text file (server log). Probably embedded. The problem is, this file is updated whenever events happen in the server (faster than half a second usually). How can I make it so the webpage displays the file in real time, meaning showing a live feed of the file?

我有一个要显示文本文件(服务器日志)的网站。应该是嵌入式。问题是,只要服务器中发生事件(通常快于半秒),就会更新此文件。我怎样才能让网页实时显示文件,这意味着显示文件的实时提要?

My guess is that it would use javascript and AJAX but my knowledge on both are pretty limited. Any pointers and help would be appreciated :)

我的猜测是它会使用 javascript 和 AJAX,但我对两者的了解都非常有限。任何指针和帮助将不胜感激:)

回答by LeeR

Using jQuery, you could do the following

使用 jQuery,您可以执行以下操作

setInterval(function() {
    $('#element').load('/url/to/file');
}, 1000);

Would refresh the div with ID elementwith the file contents every 1 second

element每 1 秒用 ID和文件内容刷新 div

回答by neo

My answer uses PHP and Ajax though changing to ASP or any other language wont be hard.
In the head

我的答案使用 PHP 和 Ajax,但更改为 ASP 或任何其他语言并不难。
在头上

    <script type="text/javascript">

        function Ajax()
        {
            var
                $http,
                $self = arguments.callee;

            if (window.XMLHttpRequest) {
                $http = new XMLHttpRequest();
            } else if (window.ActiveXObject) {
                try {
                    $http = new ActiveXObject('Msxml2.XMLHTTP');
                } catch(e) {
                    $http = new ActiveXObject('Microsoft.XMLHTTP');
                }
            }

            if ($http) {
                $http.onreadystatechange = function()
                {
                    if (/4|^complete$/.test($http.readyState)) {
                        document.getElementById('ReloadThis').innerHTML = $http.responseText;
                        setTimeout(function(){$self();}, 1000);
                    }
                };
                $http.open('GET', 'loadtxt.php' + '?' + new Date().getTime(), true);
                $http.send(null);
            }

        }

    </script>

In the Body

在身体里

    <script type="text/javascript">
        setTimeout(function() {Ajax();}, 1000);
    </script>
    <div id="ReloadThis">Default text</div>

</body>

Now using loadtxt.php read the values of the text file

现在使用 loadtxt.php 读取文本文件的值

    <?php
        $file = "error.txt";
        $f = fopen($file, "r");
        while ( $line = fgets($f, 1000) ) {
            print $line;
        }
    ?>

回答by Bemmu

You could use jQuery .get to get the file every few seconds and update the page to show the contents.

您可以使用 jQuery .get 每隔几秒钟获取文件并更新页面以显示内容。

回答by Dunhamzzz

There are various ways of doing this...

有多种方法可以做到这一点......

You could look into long polling.

你可以看看长轮询

Stick a meta refresh tagto refresh the page every X seconds.

粘贴一个元刷新标签,每 X 秒刷新一次页面。

tail -f /path/to/log.login terminal will open a live preview of the last few lines of that file - this is what I do if I need to read the error logs as I debug.

tail -f /path/to/log.log在终端中将打开该文件最后几行的实时预览 - 如果我需要在调试时读取错误日志,这就是我所做的。

Or simply refresh the page manually as you go, it might be annoying having the page change it's contents automatically.

或者只是在您进行时手动刷新页面,让页面自动更改其内容可能会很烦人。

As you have said your file is very large, I would use the PHP file()function to just grab the first X amount of lines from a file to keep bandwith down and readability up!

正如您所说,您的文件非常大,我将使用 PHPfile()函数从文件中抓取前 X 行,以降低带宽和提高可读性!

回答by Chris

Others have talked about loading the log file every refresh but depending on the size of the file this migth be a bad idea. You might want to create a server side page that will read the log file and keep track of how much of it has already been given to you and only give you the new bits. If its a 10k file it would be annoying (and potentially laggy) to have this transferred to you every second.

其他人谈到每次刷新时加载日志文件,但根据文件的大小,这可能是一个坏主意。您可能希望创建一个服务器端页面,该页面将读取日志文件并跟踪已提供给您的文件数量,并且只为您提供新的位。如果它是一个 10k 文件,那么每秒将其传输给您会很烦人(并且可能会滞后)。

Otherwise other people seem to have covered most of the client side stuff.

否则其他人似乎已经涵盖了大部分客户端的东西。