阅读txt文件实时的JavaScript [英] Read txt file real time with Javascript
本文介绍了阅读txt文件实时的JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道是否有某种方式来读取从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();
这篇关于阅读txt文件实时的JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文