我會設置你的PHP到功能可按像一個RESTful API端點。
而且就像您的問題所示,我可以使用jQuery的$.getJSON以及setInterval在您希望的任何時間段之後刷新圖像。
這裏是你如何能做到這一點(有很多,很多其他方式)
的一個實例:
PHP文件(GET-刷新imgs.php):
<?php
$dir = "uploads/*";
echo json_encode(glob($dir));
?>
JavaScript文件(AJAX -refresh.js):
$(function(){
var intervalInMiliseconds = 10000;
//comment out if you don't want to load images immediately
refreshImgs();
setInterval(function(){
refreshImgs();
}, intervalInMiliseconds);
function refreshImgs(){
var container = $('body'); //use whatever container you wish. I'm using body.
$.getJSON('get-refresh-imgs.php', function(data){
container.empty();
data.forEach(function(el, indx, ary){
container.append('<img src="' + el + '"/>');
});
});
}
});
然後,在你的HTML文件中包含的:
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script type="text/javascript" src="ajax-refresh.js"></script>
有無PHP輸出圖像的URL的陣列。在客戶端,一個'setInterval()'函數可以一次選取一個數組項,使用'document.createElement()'構建一個圖像,並將其附加到當前存放上述代碼的任何容器中。 –