我有这个代码,每次按下按钮都会改变图像。我必须改变它,以便在不按下按钮的情况下循环显示图像。我不知道从哪里开始将其改为自由移动周期。请帮忙
<script type='text/javascript'>
var TL = new Array("Traffic Lights Red (4).jpg",
"Traffic Lights RY.jpg",
"Traffic Light Green.jpg",
"Traffic Lights Yellow.jpg",
"Traffic Lights Red (4).jpg")
var Amount = 0;
function lightCycle()
{
if(++Amount == 5)
Amount = 0;
document.images.banner.src = TL[Amount];
}
</script>
</head>
<body>
<img src="Traffic Lights Red (4).jpg" name="banner">
<form>
<input type="button" value="Change lights" name="Change lights" onclick="lightCycle()">
</form>
</body>
</html>
答案 0 :(得分:1)
您可以添加以下代码,这将使其自行循环
setInterval(lightCycle, 2000);
这会每2000毫秒触发一次lightCycle函数:)