admin管理员组文章数量:1277900
NB: I've had a look here for how to change the images on a timer, as oppose to using buttons, but it didn't cater to my code style.
As I'm new to HTML and JS I still don't understand all the aspects and got a little confused on which part they were referring to in translation to how I wrote my code.
CODE:
<!DOCTYPE html>
<html>
<body>
<img id="image" src="blank_light.jpg" style="width:100px">
<p></p>
<button class="change-image">Change Lights</button>
<script>
var imageSources = ["red_light.jpg", "red_and_yellow_light.jpg", "yellow_light.jpg", "green_light.jpg"]
var buttons = document.getElementsByClassName("change-image")[0];
var index = 0;
buttons.addEventListener('click', function() {
if (index === imageSources.length) {
index = 0;
}
document.getElementById("image").src = imageSources[index];
index++;
});
</script>
</body>
</html>
I need to remove the buttons and have the images change on a timed basis instead. Ideally every 2 seconds.
Thanks in advance.
NB: I've had a look here for how to change the images on a timer, as oppose to using buttons, but it didn't cater to my code style.
As I'm new to HTML and JS I still don't understand all the aspects and got a little confused on which part they were referring to in translation to how I wrote my code.
CODE:
<!DOCTYPE html>
<html>
<body>
<img id="image" src="blank_light.jpg" style="width:100px">
<p></p>
<button class="change-image">Change Lights</button>
<script>
var imageSources = ["red_light.jpg", "red_and_yellow_light.jpg", "yellow_light.jpg", "green_light.jpg"]
var buttons = document.getElementsByClassName("change-image")[0];
var index = 0;
buttons.addEventListener('click', function() {
if (index === imageSources.length) {
index = 0;
}
document.getElementById("image").src = imageSources[index];
index++;
});
</script>
</body>
</html>
I need to remove the buttons and have the images change on a timed basis instead. Ideally every 2 seconds.
Thanks in advance.
Share Improve this question edited May 23, 2017 at 11:45 CommunityBot 11 silver badge asked Feb 2, 2017 at 13:32 freddie.bumderfreddie.bumder 151 gold badge1 silver badge9 bronze badges 2- It's the same link he posted in the question, it's not the same problem – Mattia Nocerino Commented Feb 2, 2017 at 13:37
- @ACOMIT001 Like Mattia kindly said, it's a different problem as the link I posted has a different original format as me. There's no point writing a bunch of code if I don't understand what it means. – freddie.bumder Commented Feb 2, 2017 at 13:40
2 Answers
Reset to default 8You need to use setInterval function to change image after every 2 sec
<img id="image" src="blank_light.jpg" style="width:100px">
<p></p>
<script>
var imageSources = ["red_light.jpg", "red_and_yellow_light.jpg", "yellow_light.jpg", "green_light.jpg"]
var index = 0;
setInterval (function(){
if (index === imageSources.length) {
index = 0;
}
document.getElementById("image").src = imageSources[index];
index++;
} , 2000);
</script>
CodePen : http://codepen.io/anon/pen/zNWMJK
/CoffeeScript
$(document).ready ->
imageSources = ["image1.jpg", "image2.png", "image3.png", "image4.png", "image5.png"]
index = 0
setInterval ( ->
if index == imageSources.length
index = 0
$('#image-test').attr('src', imageSources[index]);
index++
return
), 2000
本文标签: How To Automatically Change An HTML Image Using JavaScriptStack Overflow
版权声明:本文标题:How To Automatically Change An HTML Image Using JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741223608a2361441.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论