图像不闪烁

时间:2012-01-29 06:33:26

标签: php javascript html

我有一个显示图像的PHP脚本(根据当前时间),我希望它能够刷新图像,所以我尝试添加一个脚本来执行此操作,但它有些不起作用。请看一看。我很感谢你的帮助。提前谢谢。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

</head>

<body onload="startit();">
<?php
$hour = date('H', time()); // Returns the hours of the server time, from 0 to 23.

switch ($hour) {
case '0':
$img = '0.jpg';
break;
case '1':
$img = '0.jpg';
break;
// ... and so on...
default:
$img = '1.jpg';
}

?>

<img src="<?php echo $img; ?>" id="mydiv" />
<script language=javascript>
/*This script written by Chris Stahl*/
/*It may be used and modified by */
/*webmaster's for private use.      */
function startit() {
window.setInterval("changecolor()",100);
}

function changecolor() {
var rndred=Math.floor(256*Math.random());
var rndgreen=Math.floor(256*Math.random());
var rndblue=Math.floor(256*Math.random());
var colorarray=[rndred, rndgreen, rndblue];
var hexcolor="#";
for(i=0;i<3;i++) {
 hexcolor=hexcolor +""+ converttohex(Math.floor(colorarray[i]/16));
 hexcolor=hexcolor +""+ converttohex(colorarray[i]%16);
}
document.getElementById("mydiv").style.color=hexcolor;
}
function converttohex(num) {
switch (num) {
case 10: return "A";
case 11: return "B";
case 12: return "C";
case 13: return "D";
case 14: return "E";
case 15: return "F";
default: return num;
}
}
</script>


</body>
</html>

1 个答案:

答案 0 :(得分:3)

这将有效

http://jsfiddle.net/mplungjan/9Kdh5/

<body>
  <div id="myDiv"><img src="http://rd.cas.de/tim/native/image.png" id="myImg" /></div>
</body>

<script type="text/javascript>
/* This script written by Chris Stahl */
/* It may be used and modified by     */
/* webmaster's for private use.       */

window.onload=function() {
  window.setInterval(changecolor,100);
}

function changecolor() {
  var rndred=Math.floor(256*Math.random());
  var rndgreen=Math.floor(256*Math.random());
  var rndblue=Math.floor(256*Math.random());
  var colorarray=[rndred, rndgreen, rndblue];
  var hexcolor="#";
  for(i=0;i<3;i++) {
    hexcolor=hexcolor +""+ converttohex(Math.floor(colorarray[i]/16));
    hexcolor=hexcolor +""+ converttohex(colorarray[i]%16);
  }
  document.getElementById("myDiv").style.backgroundColor=hexcolor;
}
function converttohex(num) {
  switch (num) {
    case 10: return "A";
    case 11: return "B";
    case 12: return "C";
    case 13: return "D";
    case 14: return "E";
    case 15: return "F";
    default: return num;
  }
}
</script>