阅读txt文件实时的JavaScript实时、文件、txt、JavaScript

由网友(小明不死数学难亡)分享简介:我想知道是否有某种方式来读取从Javascript实时.txt文件?我有更新每隔几秒钟的日志文件。我想从该日志文件分析一些数据,因为它的更新,并在HTML中显示。我可以干这活?非常感谢你们!I would like to know if there is some way to read a .txt file...

我想知道是否有某种方式来读取从Javascript实时.txt文件? 我有更新每隔几秒钟的日志文件。我想从该日志文件分析一些数据,因为它的更新,并在HTML中显示。 我可以干这活? 非常感谢你们!

I would like to know if there is some way to read a .txt file from Javascript in real-time? I have a log file that updates every few seconds. I want to parse some data from that log file as it updates and display it in a html. Can I do this live? Thanks a lot guys!

推荐答案

假设该文件是某个地方公开访问,你可以有一个JavaScript函数,它使一个Ajax请求每隔几秒钟就可以读取文件。类似如下:

Assuming the file is somewhere publicly accessible, you can have a javascript function which makes an AJAX request every few seconds to read the file. Something like the following:

function getLog() {
    $.ajax({
        url: 'logfile.txt',
        dataType: 'text',
        success: function(text) {
            $("#containerDiv").text(text);
            setTimeout(getLog, 30000); // refresh every 30 seconds
        }
    })
}

getLog();
阅读全文

相关推荐

最新文章