我尝试在 setInterval()
中使用 get_file_contents()
来重复更新一些显示文件内容的文本。代码如下:
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<body>
<div id="theDiv"></div>
</body>
<script>
$("document").ready(function(){
setInterval(function(){
$("#theDiv").text("<?php echo file_get_contents('file.txt'); ?>");
console.log("<?php echo file_get_contents('file.txt'); ?>")
},500);
});
</script>
问题是:在我重新加载页面之前,文本似乎不会更新。
最佳答案
file_get_contents()
未设置为通过 setInerval
重新加载。您混淆了客户端 JavaScript 和服务器端 PHP。
您可能需要以下内容:
$("document").ready(function(){
setInterval(function(){
$("#theDiv").load('file.txt');
},500);
});
关于javascript - setInterval() 中的 PHP file_get_contents() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20794444/