Javascript和PHP解析

Javascript和PHP解析,javascript,php,html,ajax,Javascript,Php,Html,Ajax,我有个问题 我有一个应用程序,它可以将最多1000个jpg文件写入位于web服务器中的文件夹 我必须创建一个播放器来读取这些文件。 共有4个按钮,第一个按钮跳到最后一个图像,第二个按钮跳到上一个图像,第三个按钮跳到下一个图像,第四个按钮跳到第一个图像。 存储图像的文件夹总是在更改,最旧的图像会被删除,并替换为每秒出现的新图像 我发布的代码有效。唯一的问题是图像不新鲜。 如果出现新图像,您将无法看到它们 我想知道我是应该使用一个每次重新加载图像的系统(我会使用AJAX),还是我自己制作的形式可以

我有个问题

我有一个应用程序,它可以将最多1000个jpg文件写入位于web服务器中的文件夹

我必须创建一个播放器来读取这些文件。 共有4个按钮,第一个按钮跳到最后一个图像,第二个按钮跳到上一个图像,第三个按钮跳到下一个图像,第四个按钮跳到第一个图像。 存储图像的文件夹总是在更改,最旧的图像会被删除,并替换为每秒出现的新图像

我发布的代码有效。唯一的问题是图像不新鲜。 如果出现新图像,您将无法看到它们

我想知道我是应该使用一个每次重新加载图像的系统(我会使用AJAX),还是我自己制作的形式可以

对不起,我的英语很差。 多谢各位

<html>
<head>
<script language="Javascript">

var indice;
arr = new Array();

function setarray(set_arr)
{
arr = set_arr;
chiamata(0);
}


function chiamata(tipo) 
{
var immagine = document.getElementById('snap');



if (tipo==0)
{
indice = arr.length-1;

}else if (tipo==1)
{
indice = indice-1;

}else if (tipo==2)
{
indice = indice+1;

}else if (tipo==3)
{

indice = 0;

}

if (indice==arr.length-1)
{
document.getElementById("next").disabled = true;
document.getElementById("before").disabled = false;
}else if (indice==0)
{ 
document.getElementById("before").disabled = true; 
document.getElementById("next").disabled = false;
}else
{
document.getElementById("before").disabled = false; 
document.getElementById("next").disabled = false;
}


immagine.src='/images/' + arr[window.indice] +'?rand=' + Math.random();

}

</script>


<?php

exec('ls /var/www/images/ -rt',$arr_images);
$js_array = json_encode($arr_images);

echo "</head>";
echo "<body onload='setarray($js_array);'>";

?>

<form>
<table border="1">
<tr>
<td colspan="4"><img id="snap" src="" width="450" height="360"></td>
</tr>
<tr>
<td><button type="button" onclick="chiamata(0)"> << </button></td>
<td><button type="button" id="before" onclick="chiamata(1)"> > </button></td>
<td><button type="button" id="next" onclick="chiamata(2)"> < </button></td>
<td><button type="button" onclick="chiamata(3)"> >> </button></td>
</tr>
</table>
</form>

<br>

</body>
</html>

印度变种;
arr=新数组();
函数集合数组(集合数组)
{
arr=设置_arr;
chiamata(0);
}
基亚马塔职能(tipo)
{
var immagine=document.getElementById('snap');
如果(tipo==0)
{
indice=arr.length-1;
}否则如果(tipo==1)
{
印度=印度-1;
}否则如果(tipo==2)
{
指数=指数+1;
}否则如果(tipo==3)
{
指数=0;
}
如果(标记==阵列长度-1)
{
document.getElementById(“next”).disabled=true;
document.getElementById(“before”).disabled=false;
}else if(指标==0)
{ 
document.getElementById(“before”).disabled=true;
document.getElementById(“next”).disabled=false;
}否则
{
document.getElementById(“before”).disabled=false;
document.getElementById(“next”).disabled=false;
}
immagine.src='/images/'+arr[window.indice]+'?rand='+Math.random();
}

更改
img
src
属性足以使其重新加载新图像。我相信您的代码中还存在其他逻辑/语法问题,这些问题阻碍了代码的正确执行。您的PHP代码在第一次生成页面时执行一次。之后,PHP的输出成为页面的静态部分。您需要使用AJAX调用定期轮询服务器以获取更新的文件列表。另外。不需要使用函数调用来设置数组。PHP是在js引擎可能开始运行之前执行的<代码>var arr=是所有需要的。是的,我知道它执行过一次,但我问了,因为我想知道我制作它的形式是否正确,以及是否有其他选择。我将更改您发布的表单中的代码。我将尝试使用AJAX,但我必须检查是否需要它。非常感谢。