Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5984103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:19:35+00:00 2026-05-22T22:19:35+00:00

I have been working on an image gallery in PHP and Javscript for a

  • 0

I have been working on an image gallery in PHP and Javscript for a long time. The “selling point” of the gallery is that it pre-loads images so when you switch to the next image, you don’t have to reload the page and it is almost instantaneous.

The problem is that currently when you switch to a photo that has not been loaded, it loads every image before it. I want to load the image that should be currently showed to the viewer. But even when the console says the current image is loaded (console.log messages I set up), it does not switch until all the other photos before it have been loaded.

An example page in question.
I will put some of the most important code here because I don’t want to give all the code.

This function load_image adds the next photo to be loaded to the hidden_container. The problem is that when the current_photo has loaded, it is not changed. You can test this in the page by viewing the console and using the arrow keys to go all the way to the last photo.

    function load_image() {
    // find the next photo to load
    var next_to_load = next_photo_to_load();
    //stop condition:
    if (next_to_load == null)
        return false;

    if (img[current_photo]['loaded'] == 0) {
        console.log("current_photo: "+current_photo);
        next_to_load = current_photo;
        console.log("next_to_load: "+next_to_load)
    }
    else {
        console.log("*current_photo loaded*");
        $("main_img").src = img[current_photo]['img'];
    }

    img[next_to_load]['loaded'] = 1;
    var url = img[next_to_load]['img'];
    var image = new Image();
    image.src = url;
    image.onload = function() {
        load_image();
    };

    document.getElementById("hidden_container").appendChild(image);
}

Also, load_image is initially called when the first photo is loaded with this code. (I included this because it explains the extra console log).

img[current_photo]['loaded'] = 1;
console.log("loaded:"+current_photo);
load_image();

Thanks for reading all of this. I know it is long.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T22:19:36+00:00Added an answer on May 22, 2026 at 10:19 pm

    You can try something like this, I have used this in a mobile gallery I have recently developed.

    var imageList = $('#imagelist');   //id of div tag
    imageList.empty();
    var imageFiles = '<?=$images_js?>';
    imageFiles = $.parseJSON(imageFiles);  //convert to json for javascript readability
    
    var images = [];
    for(i = 0; i<imageFiles.length; i++){
        var image = document.createElement('img');
        image.src = imageFiles[i];
        images.push(image);
    }
    
    var count = imageFiles.length;
    var i = 0;
    
    imageList.append(images[i]);
    

    You can see I have used a php variable within the javascript. I have done this so I can have the php file which is included with this javascript dynamically read the contents of the folder.

    the php file is very small and consists of:

    <?php
    
    $imagesDir = 'gallery/img/';
    $images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
    
    $images_js = json_encode($images);
    ?>
    

    Here is the jquery in action:

    $('#contentPlaylist').live('swipeleft swiperight',function(event){
    
        if (event.type == "swipeleft") {
            imageList.fadeOut('slow', function(){
                imageList.empty();
    
                if(i != 0){
    
                    i--;
                    imageList.append(images[i]);            
                    imageList.fadeIn('slow');
                }
    
    
                else{
                    i = count - 1;
                    imageList.append(images[i]);            
                    imageList.fadeIn('slow');
    
                }
            });
        }
        if (event.type == "swiperight") {
                imageList.fadeOut('slow', function(){
                    imageList.empty();
                    i++;        
                    if(i < count){
    
                        imageList.append(images[i]);            
                        imageList.fadeIn('slow');
                    }
                    else{
    
                        imageList.append(images[0]);
    
                        imageList.fadeIn('slow');
                        var reset = 0;
                        i = reset;
                    }
                });
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok I have been working on this gallery for some time and I keep
I have been working on developing an image gallery website in yii framework. I
I have an android application i have been working on that downloads an image
I'm working on an image gallery that looks up all of the images that
OK, I have been working on a random image selector and queue system (so
I have been working with a string[] array in C# that gets returned from
I have been working on some legacy C++ code that uses variable length structures
I have been working with Struts for some time, but for a project I
I have been working on an image gallary. When the user uploads an Image
I have a simple CSS help popup that's been working well for me in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.