0

I have a website that displays from an array's index a shuffled ordering of images, one by one. I'm looking for a way to check the images all at once with a simple function, using onerror most likely, to make sure the images still exist at their locations, and to alert me if they don't, but I'm not sure how to do it. I've tried fiddling around with forEach(). I'm not sure what I'm doing wrong or right.

var backInfo = [
{ image: "https://dfep0xlbws1ys.cloudfront.net/thumbs71/df/71dfd3bb82eb215b00dd0bf1eb1be93d.jpg?response-cache-control=max-age=2628000",
artist: "http://loish.net/dawn/",
caption: "Breathe by Lois Van Baarle aka Loish" },

{ image: "http://66.media.tumblr.com/0d5f88148461e33581ca4120d02d38d4/tumblr_nn8q8yPofl1s8p1hwo1_1280.jpg",
  artist: "http://cobaltplasma.tumblr.com/post/117141857207/finally-finished-the-companion-piece-to-another",
  caption: "The Storm King by Denjin108" }, . . .

It goes on like that for quite some time. This next part is a shuffle that randomizes the images uniquely, which I didn't write.

function shuffle(array) {
  var m = array.length, t, i;
  while (m) {
    i = Math.floor(Math.random() * m--);
    t = array[m];
    array[m] = array[i];
    array[i] = t;
  }
 return array;
}

And then this part gets the keys so, one by one, when an image is clicked, it performs the pop() function on variable mix. It took me a while to learn that Object.keys() was what I was looking for to do that.

var mix = shuffle(Object.keys(backInfo));

This is my JavaScript in total: http://www.dreamquest.io/javascript.js

And the live webpage: http://www.dreamquest.io/

resriel
  • 57
  • 6
  • http://stackoverflow.com/a/18837818 - This is how you would check if an image exists or not. Keep in mind it also preloads the image, so you wouldn't want to run it on all of the images. Only the one that you are about to show. – Matis Lepik Jun 23 '16 at 20:47
  • Yeah, I use that to check them individually. The problem is never knowing if the images toward the middle or end of the presentation are broken without going through the whole thing, and the whole thing down the road will take about an hour to go through most likely. I figure there must be a way to check for an error without preloading an image all the way. I'm willing to use jQuery or even something else if I have to. I'd just like to take from the array I've already defined in the JS. – resriel Jun 23 '16 at 20:55
  • I don't have the time to analyze your code to see what exactly you're trying to do. But if you're displaying the images one at a time, there is no reason you need to know if any image other than the next one exists. If you're about to show an image, check if *that* one exists. If it doesn't exist, discard it and try to show the next one in the list. – Matis Lepik Jun 23 '16 at 20:58
  • Trying to make it easy to check my million-mile long list of images and return their caption values to an alert without having to go through the presentation manually every single day. With image fades and delays, by the time I've added say a hundred images, it'll take more than ten minutes or longer to check. – resriel Jun 23 '16 at 21:02

0 Answers0