Skip to main content

How to show a spinner while loading an image via JavaScript


I'm currently working on a web application which has a page which displays a single chart (a .png image). On another part of this page there are a set of links which, when clicked, the entire page reloads and looks exactly the same as before except for the chart in the middle of the page.



What I want to do is when a link is clicked on a page just the chart on the page is changed. This will speed things up tremendously as the page is roughly 100kb large, and don't really want to reload the entire page just to display this.



I've been doing this via JavaScript, which works so far, using the following code




document.getElementById('chart').src = '/charts/10.png';



The problem is that when the user clicks on the link, it may take a couple of seconds before the chart changes. This makes the user think that their click hasn't done anything, or that the system is slow to respond.



What I want to happen is display a spinner / throbber / status indicator, in place of where the image is while it is loading, so when the user clicks the link they know at least the system has taken their input and is doing something about it.



I've tried a few suggestions, even using a psudo time out to show a spinner, and then flick back to the image.



A good suggestion I've had is to use the following




<img src="/charts/10.png" lowsrc="/spinner.gif"/>



Which would be ideal, except the spinner is significantly smaller than the chart which is being displayed.



Any other ideas?


Source: Tips4allCCNA FINAL EXAM

Comments

  1. I've used something like this to preload an image and then automatically call back to my javascript when the image is finished loading. You want to check complete before you setup the callback because the image may already be cached and it may not call your callback.

    function PreloadImage(imgSrc, callback){
    var objImagePreloader = new Image();

    objImagePreloader.src = imgSrc;
    if(objImagePreloader.complete){
    callback();
    objImagePreloader.onload=function(){};
    }
    else{
    objImagePreloader.onload = function() {
    callback();
    // clear onLoad, IE behaves irratically with animated gifs otherwise
    objImagePreloader.onload=function(){};
    }
    }
    }

    ReplyDelete
  2. Use the power of the setTimeout() function (More info) - this allows you set a timer to trigger a function call in the future, and calling it won't block execution of the current / other functions (async.).

    Position a div containing the spinner above the chart image, with it's css display attribute set to none:

    <div>&nbsp;<img src="spinner.gif" id="spinnerImg" style="display: none;" /></div>


    The nbsp stop the div collapsing when the spinner is hidden. Without it, when you toggle display of the spinner, your layout will "twitch"

    function chartOnClick() {
    //How long to show the spinner for in ms (eg 3 seconds)
    var spinnerShowTime = 3000

    //Show the spinner
    document.getElementById('spinnerImg').style.display = "";

    //Change the chart src
    document.getElementById('chart').src = '/charts/10.png';

    //Set the timeout on the spinner
    setTimeout("hideSpinner()", spinnerShowTime);
    }

    function hideSpinner() {
    document.getElementById('spinnerImg').style.display = "none";
    }


    Shameless self promotion of my spinner in action

    ReplyDelete
  3. Aside from the lowsrc option, I've also used a background-image on the img's container.

    ReplyDelete
  4. put the spinner in a div the same size as the chart, you know the height and width so you can use relative positioning to center it correctly.

    ReplyDelete
  5. @iAn's solution looks good to me. The only thing I'd change is instead of using setTimeout, I'd try and hook into the images 'Load' event. This way, if the image takes longer than 3 seconds to download, you'll still get the spinner.

    On the other hand, if it takes less time to download, you'll get the spinner for less than 3 seconds.

    ReplyDelete
  6. You could show a static image that gives the optical illusion of a spinny-wheel, like these.

    ReplyDelete
  7. Use CSS to set the loading animation as a centered background-image for the image's container.

    Then when loading the new large image, first set the src to a preloaded transparent 1 pixel gif.

    e.g.


    document.getElementById('mainimg').src = '/images/1pix.gif';
    document.getElementById('mainimg').src = '/images/large_image.jpg';


    While the large_image.jpg is loading, the background will show through the 1pix transparent gif.

    ReplyDelete
  8. I would add some random digits to avoid the browser cache.

    ReplyDelete
  9. Building on Ed's answer, I would prefer to see something like:

    function PreLoadImage( srcURL, callback, errorCallback ) {
    var thePic = new Image();

    thePic.onload = function() {
    callback();
    thePic.onload = function(){};
    }

    thePic.onerror = function() {
    errorCallback();
    }
    thePic.src = srcURL;
    }


    Your callback can display the image in its proper place and dispose/hide of a spinner, and the errorCallback prevents your page from "beachballing". All event driven, no timers or polling, plus you don't have to add the additional if statements to check if the image completed loading while you where setting up your events - since they're set up beforehand they'll trigger regardless of how quickly the images loads.

    ReplyDelete
  10. Be aware that the callback function is also called if the image src doesn't exist (http 404 error). To avoid this you can check the width of the image, like:

    if(this.width == 0) return false;

    ReplyDelete
  11. I like @duddle's jquery method but find that load() isn't always called (such as when the image is retrieved from cache in IE). I use this version instead:

    $('img.example').one('load', function() {
    $('#spinner').remove();
    }).each(function() {
    if(this.complete) {
    $(this).trigger('load');
    }
    });


    This calls load at most one time and immediately if it's already completed loading.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.