images: [
{
img: 'car_0.png',
time: 3000,
},
{
img: 'car_1.png',
time: 500,
},
{
img: 'car_2.png',
time: 500,
},
{
img: 'car_3.png',
time: 1000,
},
],
I need display image in my page. time is the time when the image should be displayed on the page. For example:
The first photo car_0.png is displayed on the page for 3 seconds. After 3 seconds it changes to the second image, which is displayed for 0.5 seconds...
I have writen this method but not working:
if(images !== undefined) {
for(let i = 0; i < images.length; i++) {
setTimeout(() => {
this.images = images[i].img
}, images[i].time + images[i + 1].time)
}
}
EDIT:
I change my method to:
if(images !== undefined) {
let time
for(let i = 0; i < images.length; i++) {
if(i > 0) {
time = time + images[i].time
}
else {
time = images[i].time
}
console.log(time)
setTimeout(() => {
this.images = images[i].img
}, time)
}
}
Now in console.log(time) I have value:
3000
3500
4000
5000
But still not working correctly, the first picture should appear immediately and be displayed for 3 seconds. Now appears after 3 seconds