Results 1 to 9 of 9

Thread: Wait for Images to finish loading

  1. #1
    Join Date
    Mar 2010
    Location
    Florida
    Posts
    512
    Thanks
    9
    Thanked 61 Times in 59 Posts

    Default Wait for Images to finish loading

    Like the title says, I am currently working on something that reads images use ajax/php from a certain folder. It grabs all the images and creates a mini gallery. The problem I am having is if they are 10k by 10k it takes forever to load (when resized).

    I also use something like this:

    $('#imageDiv').css({'background-image':'url(loader.gif)', 'background-repeat':'no-repeat', 'background-position':'center center'})

    Which should create the loading GIF. Now when the images are trying to load (massive sized ones) it slows down the page and makes it massively laggy.

    I did create something like this tho:
    Code:
    $('#imageDiv').html('<img src="" />')
    $('#imageDiv').children('img').hide()
    $('#imageDiv').children('img').attr('src',image)
    $('#imageDiv').children('img').on('load',function(){
    			$(this).fadeIn(200)
    			$(this).parent().css({'background-image':'none'})
    		})
    How can I fix that
    -DW [Deadweight]
    Resolving your thread: First Post: => EDIT => Lower right: => GO ADVANCED => Top Advance Editor drop down: => PREFIX:Resolved

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    It gets complicated. The .hide() function makes display none. The browser doesn't have to load that resource. I think most do now, but some may still not. At one point none did.

    You could perhaps load the images sequentially using javascript new Image(). I can get into more detail on that if you need it.

    An important thing to be aware of - though most browsers work just fine with:

    Code:
    $('#imageDiv').children('img').attr('src',image)
    $('#imageDiv').children('img').on('load',function(){
    			$(this).fadeIn(200)
    			$(this).parent().css({'background-image':'none'})
    		})
    This assumes the image is not cached. If it is, it may load before the onload event is assigned. You can always do:

    Code:
    $('#imageDiv').children('img').on('load',function(){
    			$(this).fadeIn(200)
    			$(this).parent().css({'background-image':'none'})
    		}).attr('src',image);
    Which makes sure the load event is assigned before the image even begins loading.

    Now, there's nothing you can do to speed up the loading of images. They will take however long that they will take. The one exception is holding back some images while others load. A popular script at one time called lazy load or something like that prevented images that were not seen (images below the bottom of the browser window) from loading until the page was scrolled to where they were located. I think that's still available, you can Google it. The advantage there is that the images that are in the viewable area of the page load faster because they don't have to compete with the unseen "below the fold" images.

    Browsers vary though, so anything simple that might work in one, might not in others.

    The conventional way of dealing with the situation you describe is to display only smaller thumbnail images until - well that's up to you. It could be until hover, but with mobiles, that's ambiguous, so click is popular, like with Lightbox type scripts.

    BTW - Resizing images has no effect on their load time unless you are actually changing their byte size on the server side. If that's what's taking so long, you would be far better off with thumbnails prepared ahead of time. Also how is an image 10K by 10K? K is usually a measure of bytes not pixels. What do you really mean by that? 100k? Something else?
    Last edited by jscheuer1; 05-08-2015 at 10:06 AM.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Mar 2010
    Location
    Florida
    Posts
    512
    Thanks
    9
    Thanked 61 Times in 59 Posts

    Default

    Thanks again. I think my issue is how I am trying to make it appear. The sizes of the files are massive but I uploaded it to a server so you can see what I am talking about:
    http://thebcelements.com/dhtml/main/...lery%20Remake/

    Also I did mean 3k x 2k pixels and about 4MBs
    Last edited by Deadweight; 05-08-2015 at 07:13 PM.
    -DW [Deadweight]
    Resolving your thread: First Post: => EDIT => Lower right: => GO ADVANCED => Top Advance Editor drop down: => PREFIX:Resolved

  4. #4
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    Can I just ask, why do your images need to be so big? Could you not display an optimised preview image but then provide the bigger/HD version as a download link? That would help with load speeds (perceived and actual) and in turn help with visitors perception of the site/service (considerate, fast - to help them achieve their goals quicker, less likely to crash browsers, lots of other feel-good about not having to wait while content chugs onto the screen)
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  5. #5
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    Yup - I clicked your link in iPhone and the browser pooped
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  6. #6
    Join Date
    Mar 2010
    Location
    Florida
    Posts
    512
    Thanks
    9
    Thanked 61 Times in 59 Posts

    Default

    Because I am grabbing the photos with php and if they cannot be edited due to legal reasons.
    -DW [Deadweight]
    Resolving your thread: First Post: => EDIT => Lower right: => GO ADVANCED => Top Advance Editor drop down: => PREFIX:Resolved

  7. #7
    Join Date
    Mar 2010
    Location
    Florida
    Posts
    512
    Thanks
    9
    Thanked 61 Times in 59 Posts

    Default

    Anyways I did create something that processes the same but I am still getting the loading error (prolly due to the fact that I am still loading them incorrectly).
    HTML:
    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Basic Outline</title>
    
    <link rel="stylesheet" href="css/main.css" />
    
    <script src="js/jquery.js"></script>
    <script src="js/loader.js"></script>
    <script src="js/createdfunctions.js"></script>
    <script src="js/functions.js"></script>
    <script src="js/eventfunctions.js"></script>
    
    </head>
    <body>
    <div id="progressBar"><div class="s"></div></div>
    <div id="testDiv"></div>
    </body>
    </html>
    CSS:
    Code:
    * { padding:0px; margin:0 auto;}
    #progressBar { width:100%; margin-top:2px; border:2px solid black; height:20px; position:relative;}
    .s { height:100%; width:0%; position:absolute; left:0px; top:0px; background-color:#000000;}
    JQUERY:
    Code:
    $(function(){
    	$('#testDiv').createGallery(['grabgallery.php','galleries', false])
    })
    $.fn.createGallery = function(args){
    
    	var $id = this;
    	$.ajax({
    		type: 'POST',
    		async: false,
    		url: args[0],
    		data: {location: args[1]},
    		dataType: "json",
    		success: function(response){
    			$id.loadGallery(response, args[1], args[2])
    		}//if success
    	})	
    }
    
    var loaded = 0;
    var maxloaded = 0;
    var images = Array()
    var folder = ""
    
    $.fn.loadGallery = function(imageList, dir, rot){
    	var $id = $(this)
    	folder = imageList[0];
    	images = imageList[1][0];
    	
    	maxloaded = images.length;
    	$.each(images, function(i){
    		loadImage(folder+'/'+images[i], 200, 200, $id)
    	})
    
    }
    
    function loadImage(img, w, h, id){
    	$("<img />").attr('src', img).on('load',function() {
            if (!this.complete || typeof this.naturalWidth == "undefined" || this.naturalWidth == 0) {
                alert('broken image!');
            } else {
    			loaded++;
    			id.html(loaded)
    			if(loaded==maxloaded){
    				id.html('')
    				$.each(images, function(i){
    					$(id).append('<img src="'+folder+'/'+images[i]+'" style="width:200px; height:200px;"></img><br />');
    				})
    			}
    			$('#progressBar').children('.s').animate({'width':parseInt(loaded/maxloaded*100)+'%'},0)
                //$(this).width(w).height(h).appendTo(id);
            }
        });
    }
    -DW [Deadweight]
    Resolving your thread: First Post: => EDIT => Lower right: => GO ADVANCED => Top Advance Editor drop down: => PREFIX:Resolved

  8. #8
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,881
    Thanks
    49
    Thanked 266 Times in 258 Posts
    Blog Entries
    56

  9. #9
    Join Date
    Mar 2010
    Location
    Florida
    Posts
    512
    Thanks
    9
    Thanked 61 Times in 59 Posts

    Default

    For me it doesn't work. Maybe I am doing it wrong but don't i make a new class:

    $t = new scratch_utils_imgresize;
    $t->resize(info in here);

    Because nothing happens
    -DW [Deadweight]
    Resolving your thread: First Post: => EDIT => Lower right: => GO ADVANCED => Top Advance Editor drop down: => PREFIX:Resolved

Similar Threads

  1. Finish Java Application
    By ojsimon in forum General Paid Work Requests
    Replies: 1
    Last Post: 01-21-2010, 08:24 AM
  2. Loading...Please Wait message on form submit
    By JasonDFR in forum JavaScript
    Replies: 4
    Last Post: 10-14-2009, 12:35 AM
  3. Please help me finish my endless search!
    By teapot in forum Looking for such a script or service
    Replies: 1
    Last Post: 03-12-2008, 03:26 PM
  4. Please wait, or Loading window?
    By dstoltz in forum Looking for such a script or service
    Replies: 4
    Last Post: 08-14-2007, 05:38 PM
  5. Replies: 1
    Last Post: 06-22-2007, 06:36 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •