简体   繁体   English

用php内容刷新div

[英]Refreshing a div with php contents

I am using a webcam to take photos for a learning project I am currently working on and trying to display the images that have just been taken. 我正在使用网络摄像头为当前正在研究的学习项目拍照,并试图显示刚刚拍摄的图像。

When a photo is taken its placed in a folder and wishing to display all the photos taken, inside the <div> I am needing to refresh will be the PHP code to display them. 拍摄照片后,将其放在文件夹中并希望显示所有照片,在我需要刷新的<div>内部将是显示它们的PHP代码。

I have created a simple div with an PHP file named testdata.php which echo's a random number to see if i could get this to work. 我用一个名为testdata.php的PHP文件创建了一个简单的div,该文件回显了一个随机数,以查看是否可以正常工作。

I originally pulled the script from a similar question on stackoverflow, but cant get it to work. 我最初是从关于stackoverflow的类似问题中提取脚本的,但无法使其正常工作。

<script type="text/javascript">
          $(document).ready(function () {
             $('#mydiv').delay(10000).load('testdata.php');
          });
      </script>


 <div id="mydiv"></div>

The problem is it displays the contents of the file but it doesn't refresh it 问题是它显示了文件的内容,但没有刷新它

here is the code for testdata.php 这是testdata.php的代码

<?php
echo(rand(10,100));
?>

Use this: 用这个:

setInterval(function(){
     $('#mydiv').load('testdata.php');
}, 3000);

This will execute the function every 3 seconds. 这将每3秒执行一次功能。

Read more about setInterval here 在此处阅读有关setInterval的更多信息

Your code is only run once after the page loaded. 页面加载后,您的代码仅运行一次。 You will need to execute it periodically: 您将需要定期执行它:

$(document).ready(function () {
    setInterval(function() {
        $('#mydiv').load('testdata.php');
    }, 10000);
});

This will execute the load-function once every 10000 milliseconds. 这将每10000毫秒执行一次加载功能。

You can use JavaScript's setInterval to periodically call a function to load contents from the server. 您可以使用JavaScript的setInterval定期调用函数以从服务器加载内容。

So if you want to execute the PHP script, say every 5 seconds, then you should use something like this: 因此,如果您想执行PHP脚本(例如每5秒执行一次),则应使用如下代码:

$(document).ready(function () {
             setInterval(function(){ $('#mydiv').load('testdata.php') }, 5000);
});

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

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