The image is not get loaded by this code any improvement needed? image url is stored into array for accessing that we required something?
JavaScript
x
18
18
1
<script>
2
var images = ["lap2.png", "lap1.png", "tv1.png", "tv2.png"];
3
var i;
4
5
function slides() {
6
for (i = 0; i < 4; i++) {
7
setInterval(function() {
8
document.getElementById('slider').src = "" + images[i];
9
}, 8000);
10
}
11
12
}
13
</script>
14
<!--HTML CODE-->
15
<p id="slide"><img src="lap1.png" id="slider" onload="this.onload=null;
16
this.src=slides();" multiple>
17
</p>
18
Advertisement
Answer
JavaScript
1
6
1
var images = ["1.png", "2.png", "3.png", "4.png"];
2
let i=0;
3
setInterval(function() {
4
i = i < images.length -1 ? i + 1 : 0;
5
document.getElementById('slide').src = `http://placehold.it/300x150?text=${images[i]}`;
6
}, 1000);
JavaScript
1
1
1
<img id="slide" src="http://placehold.it/300x150?text=1.png"></img>