1

I'm using a getJSON request to display a random image on hover. My problem is that each image is loading slowly, I believe because the JSON request is happening on each hover. Any suggestions on how I could make this request work quicker? Many thanks :)

  $.getJSON('images.json', function(result) {
    $('#imagehover').on('mouseenter', function() {
      var index = Math.floor(result.url.length * Math.random());
      $("#imageresult").html($('<img>', {src: result.url[index]}));
    });
  });
jack_
  • 29
  • 6
  • _"I believe because the JSON request is happening on each hover"_ it's not – Phil Jun 27 '19 at 00:46
  • Possible duplicate of [Preloading images with jQuery](https://stackoverflow.com/questions/476679/preloading-images-with-jquery) – Phil Jun 27 '19 at 00:49
  • How large are your images and how many do you expect to have ? – SScotti Jun 27 '19 at 00:53
  • There are about 70 images around 400kb. What if the data was stored in a variable, would the images be preloaded? – jack_ Jun 27 '19 at 00:55

0 Answers0