Skip to content

Commit

Permalink
Merge pull request #44 from avish/multiple-bindings-fix
Browse files Browse the repository at this point in the history
Use named functions for image handlers to allow multiple bindings and unbindings.
  • Loading branch information
darsain committed Dec 16, 2012
2 parents 4192ae4 + 987e806 commit aa047b8
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions jquery.imagesloaded.js
Expand Up @@ -50,6 +50,10 @@ $.fn.imagesLoaded = function( callback ) {
callback.call( $this, $images, $proper, $broken );
}
}

function imgLoadedHandler( event ) {
imgLoaded( event.target, event.type === 'error' );
}

function imgLoaded( img, isBroken ) {
// don't proceed if BLANK image, or image is already loaded
Expand All @@ -76,20 +80,18 @@ $.fn.imagesLoaded = function( callback ) {
}

// call doneLoading and clean listeners if all images are loaded
if ( $images.length === loaded.length ){
if ( $images.length === loaded.length ) {
setTimeout( doneLoading );
$images.unbind( '.imagesLoaded' );
$images.unbind( '.imagesLoaded', imgLoadedHandler );
}
}

// if no images, trigger immediately
if ( !$images.length ) {
doneLoading();
} else {
$images.bind( 'load.imagesLoaded error.imagesLoaded', function( event ){
// trigger imgLoaded
imgLoaded( event.target, event.type === 'error' );
}).each( function( i, el ) {
$images.bind( 'load.imagesLoaded error.imagesLoaded', imgLoadedHandler )
.each( function( i, el ) {
var src = el.src;

// find out if this image has been already checked for status
Expand Down

0 comments on commit aa047b8

Please sign in to comment.