1
$(document).foundation();

var image_array = new Array();
var counter = 0;
var changeBGImageInterval;
var delay = 3000; //default

$(document).ready(function(){
  console.log("Ready");

  $("#hashtagSearchForm").submit(function(event){
    var idelay = parseInt($("input[name=delaybox]").val());

    if(isNaN(idelay) === false){
      delay = idelay * 1000; // convert seconds to milliseconds (setInterval requirement)
    }else{
      delay = 3000; // set to default if no input
    }

    console.log("Fetching images with #" + $("input[name=hashtagbox]").val());
    console.log("Delay: " + delay + " milliseconds");

    // AJAX part
    $.post(
      "fetchInstagramImages.php",
      {
        'hashtag' : $("input[name=hashtagbox]").val()
      },
      function(data){
        // This is triggered after the request has been completed. This is custom code.
        // data = response data of request. In this case: echo json_decode($myArray);

        var images = JSON.parse(data);
        image_array = []; // clear array;
        $(images).each(function(count){
          image_array.push(images[count]);
        });

        // reset timed event first
        clearInterval(changeBGImageInterval);

        // for the timed event
        changeBGImageInterval = setInterval(
          function(){
            $("body").css("background-image", 'url(' + image_array[counter] +')');
            // console.log(image_array[counter]);
            counter++;
            if(counter == 20){counter = 0;}
          },
          delay // 3 seconds
        );

      }
    );
    event.preventDefault();
  });
});

How can I preload images from my slideshow using javascript? Because doesn't load fast and it doesn't look good. Any suggestions how to? Or is there anyone who could edit this? Thanks. How can I preload images from my slideshow using javascript? Because doesn't load fast and it doesn't look good. Any suggestions how to? Or is there anyone who could edit this? Thanks

1 Answer 1

1

Try preloading your images like this:

$(function(){

      //let's preload some images
      $.fn.preload = function() {
         this.each(function(){
             $('<img/>')[0].src = this;
         });
     }

     $([
       '/assets/images/prices_img/289_on.gif',
       '/assets/images/prices_img/389_on.gif',
       '/assets/images/prices_img/489_on.gif',
       '/assets/images/howitworks1.gif',
       '/assets/images/howitworks2.gif',
       '/assets/images/howitworks3.gif',
       '/assets/images/not_signs.gif',
       '/assets/images/socal_coverage.gif',
       '/assets/images/property_info_steps/a_on.gif',
       '/assets/images/property_info_steps/b_on.gif',
       '/assets/images/property_info_steps/c_on.gif',
       '/assets/images/property_info_steps/d_on.gif',
       '/assets/images/property_info_steps/e_on.gif',
       '/assets/images/property_info_steps/f_on.gif',
       '/assets/images/property_info_steps/g_on.gif',
       '/assets/images/property_info_steps/h_on.gif',
       '/assets/images/property_info_steps/i_on.gif',
       '/assets/images/property_info_steps/j_on.gif',
       '/assets/images/browseimage.gif',
       '/assets/images/ajax-loader.gif',
       ]).preload();

});

Modify the array for you images accordingly. Good luck!

EDIT: Add the following just before clearInterval(changeBGImageInterval);

         //let's preload some images
         $.fn.preload = function() {
            this.each(function(){
                $('<img/>')[0].src = this;
            });
        }

        $(image_array).preload();

So the entire modified code will look like this:

 $(document).foundation();     

 var image_array = new Array();
 var counter = 0;
 var changeBGImageInterval;
 var delay = 3000; //default     

 $(document).ready(function(){
   console.log("Ready");     

   $("#hashtagSearchForm").submit(function(event){
     var idelay = parseInt($("input[name=delaybox]").val());     

     if(isNaN(idelay) === false){
       delay = idelay * 1000; // convert seconds to milliseconds (setInterval requirement)
     }else{
       delay = 3000; // set to default if no input
     }     

     console.log("Fetching images with #" + $("input[name=hashtagbox]").val());
     console.log("Delay: " + delay + " milliseconds");     

     // AJAX part
     $.post(
       "fetchInstagramImages.php",
       {
         'hashtag' : $("input[name=hashtagbox]").val()
       },
       function(data){
         // This is triggered after the request has been completed. This is custom code.
         // data = response data of request. In this case: echo json_decode($myArray);     

         var images = JSON.parse(data);
         image_array = []; // clear array;
         $(images).each(function(count){
           image_array.push(images[count]);
         });     

         //let's preload some images
         $.fn.preload = function() {
            this.each(function(){
                $('<img/>')[0].src = this;
            });
        }

        $(image_array).preload();

         // reset timed event first
         clearInterval(changeBGImageInterval);     

         // for the timed event
         changeBGImageInterval = setInterval(
           function(){
             $("body").css("background-image", 'url(' + image_array[counter] +')');
             // console.log(image_array[counter]);
             counter++;
             if(counter == 20){counter = 0;}
           },
           delay // 3 seconds
         );     

       }
     );
     event.preventDefault();
   });
 });
Sign up to request clarification or add additional context in comments.

3 Comments

Should I change the <img/> into my array variable: image_array? The images was generated from api so they are random. What to do
Sorry :( I tried adding your code but it didn't work. Maybe it's because of the <img/> tag. Because I don't have any directories. I am generating pictures from instagram api. Then I used javascript to put it in a slideshow.
Oops! Sorry it's WORKING :D Hahaha I just had a problem with parenthesis. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.