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

时间:2015-01-09 11:34:10

标签: javascript php html

我的剧本:

<?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>';
?>

它运行得很好但是如果我想从我的网络服务器中的日志文件中获取变量,请说日志文件的名称是wget.log并且我想每隔3秒刷新一次{当我运行wget下载文件时,{1}}会不断变化吗?

1 个答案:

答案 0 :(得分:0)

阅读文件的脚本(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>