Hey, so you’re wanting to display these six images one after another at an interval of 2.5 seconds. What should happen when the final image is encountered?
Also, do you know how many images you have ahead of time?
I have 6 images in my folder and after the function finds the 6 images and display it with the interval of 2.5secs, I want the function to stop as it continues to look for a next image and giving me the error that the 7th image is not found.
Here’s a simple example. This assumes that there are 4 images titled IMG_1.jpg, IMG_2.jpg, IMG_3.jpg, IMG_4.jpg in the same folder as this script:
var pictureIndex = 1;
function displayImage() {
var newImage = new Image();
newImage.src = "IMG_" + pictureIndex + ".jpg"
newImage.onload = function(){
document.body.append(newImage);
pictureIndex++;
if(pictureIndex < 5){
setTimeout(displayImage, 2500)
}
}
}
displayImage();
It sounds like you’re making a slider of sorts, in which case another option to consider, would be to position the images one on top of the other, then show and hide them by altering their opacity.
I like all your suggestions, but the aim is to drop a new image or delete an image from the assets folder without having the error message.
The slideshow should run until the last image doesn’t matter how many images are in there and when finished, jump to the first image as the last function does.
I could hard code it, but the I believe there is a way to make it happen without getting the error message.
Try to be less complex at your code.A good define variable and setting their values it helps you to saving time, fatigue and memory. I suggest you to create an array with all images url values inside and make a loop inside it.You can erase, or adding new images or change the series of the images dynamically via this array.
yeah, that’s the thing about JS. If somehow there is a way to find the number of images first like using “length” and displaying it one by one. That would be amazing.
I know this is not common, but I’m dealing with no coding people and need to minimize the process.
Hi Pullo, that would be the ideal, but the issue is made to create online banners and platforms like Sizmek do not accept php or ajax to be uploaded.
It’s need to be just pure Javascript. It there is no way to do it, I will run a script locally that change the boolean value of the variables to false and disable the function. Thank you.
Thank you guys for all the answers. I believe you are right about the Ajax call. I just have no idea how do I do that. I’m reading about it, but can’t figure it out atm.