简体   繁体   English

每隔5分钟使用javascript从txt文件中读取数据

[英]Reading data from txt file using javascript, every 5 min

I'm trying to make the website that shows current temp at home and allows me to set the temp as I want. 我正在尝试制作显示当前临时温度的网站,并允许我根据需要设置温度。 To read temp I use raspberry pi zero and python code to save the temp in every 5 min to the .txt file. 要读取temp,我使用raspberry pi零和python代码将每5分钟的临时值保存到.txt文件中。 The problem is that I need to read current temp on my website from that file in let's say very 5 min. 问题是我需要从该文件中读取我网站上的当前临时文件,比如说5分钟。 I can use: 我可以用:

<head> <meta http-equiv="Refresh" content="s" /> </head>

But it doesn't look like a good choice so I though that I can use javascript and read data from the file. 但它看起来不是一个好选择,所以我虽然可以使用javascript并从文件中读取数据。 Unfortunately this function works only once no matter what I change in .txt file and refresh the web, output is still the same (looks like it save previous data to some kind of the global variable). 不幸的是,无论我在.txt文件中更改并刷新Web,此函数只能工作一次,输出仍然相同(看起来它将以前的数据保存到某种全局变量)。

function readTextFile()
            {
                var rawFile = new XMLHttpRequest();
                rawFile.open("GET", 'text.txt', true);
                rawFile.onreadystatechange = function ()
            {
                if(rawFile.readyState === 4)
            {
                if(rawFile.status === 200 || rawFile.status == 0)
            {
                var allText = rawFile.responseText;
                alert(allText);
            }
        }
    }
    rawFile.send(null);
}

On the left side at this picture there are data from the txt file (using php), and at the alert handler are data using javascript and submit button. 在这张图片的左侧有来自txt文件的数据(使用php),并且在警报处理程序中是使用javascript和提交按钮的数据。 These data should be the same. 这些数据应该是相同的。

在此输入图像描述

So the question is: Can I read from a .txt file when its dynamicly change? 所以问题是:当动态更改时,我可以从.txt文件中读取吗? And if so, how can I do it or what function use to do it? 如果是这样,我该怎么做或用什么功能来做呢? I don't know javascript very well. 我不太了解javascript。 I will be very grateful for help. 我将非常感谢你的帮助。

Using XHR to fetch records in a defined interval is not a good solution. 使用XHR在定义的时间间隔内获取记录不是一个好的解决方案。 I would recommend you to use JavaScript EventSource API. 我建议你使用JavaScript EventSource API。 You can use it to receive text/event-stream at a defined interval. 您可以使用它以定义的间隔接收text/event-stream You can learn more about it here - 你可以在这里了解更多相关信息 -

https://developer.mozilla.org/en-US/docs/Web/API/EventSource https://developer.mozilla.org/en-US/docs/Web/API/EventSource

For your application, you can do this - 对于您的应用程序,您可以这样做 -

JavaScript - JavaScript -

var evtSource = new EventSource('PATH_TO_PHP_FILE');

evtSource.onmessage = function(e) {
    //e.data contains the fetched data
}

PHP Code - PHP代码 -

<?php
    header('Content-Type: text/event-stream');
    header('Cache-Control: no-cache');
    $myfile = fopen("FILE_NAME.txt", "r");
    echo "data:". fread($myfile,filesize("FILE_NAME.txt"))."\n\n";
    fclose($myfile);
    flush();
?>

you need update your .txt file in some interval, for save changes, after set attribute id="textarea" 在设置属性id =“textarea”之后,您需要在某个时间间隔内更新.txt文件以进行保存更改

//use jOuery
var interval = setInterval(function(){ myFunction() },5*60*1000); // 1000=1 second

function myFunction(){
    $.get('server_address',{param1:1,param2:2},function(response){
        //if is textarea or input
        $('#textarea').val(response);
        //if is block div, span or something else
        //$('#textarea').text(response);
    });
}

server_address - is page where you read file content, and print them .... if it is php, then file "write.php" with code like server_address - 是你读取文件内容的页面,并打印它们....如果它是php,那么使用类似代码的文件“write.php”

<?php
echo(file_get_contents('some.txt'));

{param1:1,param2:2} - is object, with params, what you send to "server_address", like {action:'read',file:'some.txt'} or something other {param1:1,param2:2} - 是对象,带有params,你发送给“server_address”的内容,如{action:'read',file:'some.txt'}或其他

response - is text what is print in page on "server_address" 响应 - 是“server_address”页面中打印的文本

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

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