图像不改变后给定的时间在java脚本
images are not changing after given time in java Script
<!Doctype html>
<html>
<head>
<title>Slider</title>
<style>
</style>
</head>
<body>
<img id="text" alt="picture" src="cod4.jpg" style="width:900px;height:500px;">
<script>
var images = [];
images[0]= "cod2.jpg";
images[1]= "cod3.jpg";
images[2]= "cod4.jpg";
setInterval(changeimage,3000)
function changeimage(){
for(var i=0; i<=images.length; i++)
{
document.getElementById("text").src += images[i] ;
}
}
</script>
</body>
</html>
在java Script中给定时间后图像不会改变。我试图在3秒后改变图像,但它不工作
请告诉我这段代码有什么问题?由于
Artem Baranovskii发表了一个很棒的答案。我不确定你是否想让图像不断循环,所以我会把这个作为我的答案。
var images=["http://placehold.it/150x150","http://placehold.it/170x170","http://placehold.it/190x190"];
var i=0;
//Set image and interval when the page has loaded (Is ready).
window.onload=function(){
//Call changeimage to set the first image.
changeimage();
//Set interval
setInterval(changeimage,3000);
}
function changeimage(){
//Check if i is equal to or "||" greater than array length.
if(i==images.length||i>images.length){
//If true, reset i to 0. Back to the first image in the array
i=0;
}
//Set the image url.
document.getElementById('text').src=images[i];
//Add 1 to i.
i++;
}
<img id="text" src=""/>
源代码中的注释,但如果您有任何问题,请在下面留下评论,我会尽快回答。
我希望这对你有帮助。编码快乐!
完整源代码。
<!DOCTYPE html>
<html>
<head>
<title>Slider</title>
<script type="text/javascript">
var images=["http://placehold.it/150x150","http://placehold.it/170x170","http://placehold.it/190x190"];
var i=0;
window.onload=function(){
changeimage();
setInterval(changeimage,3000);
}
function changeimage(){
if(i==images.length||i>images.length){
i=0;
}
document.getElementById('text').src=images[i];
i++;
}
</script>
</head>
<body>
<img id="text" src=""/>
</body>
</html>
尝试以下代码:
var images = ["cod2.jpg", "cod3.jpg", "cod4.jpg"],
timer;
function changeimage() {
var nextImage = images.shift();
if (!nextImage) {
clearInterval(timer);
return;
}
document.getElementById("text").src = nextImage;
}
timer = setInterval(changeimage, 3000)
EDIT根据您的代码更改:
<!doctype html>
<html>
<head>
<title>Slider</title>
</head>
<body>
<img id="text" alt="picture" src="cod4.jpg" style="width:900px;height:500px;" />
<script>
var images = [], timer;
images.push("cod2.jpg");
images.push("cod3.jpg");
images.push("cod4.jpg");
function changeimage(){
var nextImage = images.shift();
if (!nextImage) {
clearInterval(timer);
return;
}
document.getElementById("text").src = nextImage;
}
timer = setInterval(changeimage, 3000)
</script>
</body>
</html>
相关文章:
- Java脚本时间添加
- 将时间戳转换为java中的ISO860,
- 如何使用java脚本代码计算禁用按钮的时间
- 使用java脚本获取印度标准时间
- 如何用java计算网站的响应时间
- 在PHP结果中添加复选框,并在一段时间后使用java逐个打开它们
- 如何在java中保留插入顺序,恒定时间检索和动态调整大小
- Highchart:从 java 传递系列映射时绘制日期时间图表问题
- 在Java / JavaScript中解析ISO-8601持续时间
- 在 Java 脚本中将给定时间与当前时间匹配
- 旧日期的时间戳偏差Java与Javascript(3600秒)
- 如何使用java脚本只播放当前时间大于开始时间的视频
- Java脚本时间与给定时间的比较
- 如何将时间设置为定期?JAVA
- java script setInterval设置延迟时间
- 接受并返回java.时间在我的控制器里
- Html输入类型时间和Java日期对象
- webview android最大限度地减少Java脚本加载时间
- 如何根据AM和PM使用java脚本或jquery在特定时间启动和停止发光每个按钮
- 如何在Java web应用程序中嵌入twitter时间轴的水平滚动条