Javascript - 如何从文件加载变量并每 3 秒刷新一次


Javascript - How to load variable from a file and refresh it every 3 seconds?

我的脚本:

<?php
//header("refresh: 7;");
include 'theme.php';
ceklogin();
css();
echo '<script type="text/javascript"
    src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
    $(document).ready(
            function() {
                setInterval(function() {
                    var randomnumber = Math.floor(Math.random() * 100);
                    $("#show").text(
                            "I am getting refreshed every 3 seconds..! Random Number ==> "
                                    + randomnumber);
                }, 3000);
            });
</script>';
exec('/www/openvpn_wget_status.sh');
echo '<br>';
echo file_get_contents( "/www/openvpn_status.txt" );
echo '<div id="show">';
//include 'wget.log';
//echo "<br><textarea name='"text-info'" id='"scores'" rows='"30'" cols='"90'" readonly style='"font-family: Arial;font-size: 7pt;'" >";
//$datalines = file ("wget.log");
//foreach ($datalines as $zz) {
//echo $zz; }
echo '</div>';
//echo "</textarea><br></div>";
echo '<script type="text/javascript">
       var textarea = document.getElementById("scores");
       textarea.scrollTop = textarea.scrollHeight;
      </script>';
foot();
echo '
</div>
</body>
</div>
</html>';
?>

它运行良好,但是如果我想从我的 Web 服务器中的日志文件中获取变量,假设日志文件的名称是 wget.log的,我想每 3 秒刷新一次它,因为当我运行 wget 下载文件时wget.log它会不断变化?

脚本读取

文件 (readFile.php)

<?php
//in this script, read any file(or do something else)
//whatever you output, it will be processed in ajax request from below script
echo "<pre>".file_get_contents( "/www/openvpn_status.txt" )."</pre>"; //used <pre> to keep linebreaks in file.
?>

HTML (另一个文件)

<script type="text/javascript"
    src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
    $(document).ready(
            function() {
                setInterval(function() {
                    $("#show").load("readFile.php"); //it will make ajax call to readFile.php and get output from that script and load to #show div
                }, 3000);
            });
</script>
<div id="show">
This will be refreshed every 3 seconds.
</div>