Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions jquery.imgpreload.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ if ('undefined' != typeof jQuery)
// extend jquery (because i love jQuery)
$.imgpreload = function (imgs,settings)
{
var deferred = $.Deferred();
settings = $.extend({},$.fn.imgpreload.defaults,(settings instanceof Function)?{all:settings}:settings);

// use of typeof required
Expand Down Expand Up @@ -67,16 +68,29 @@ if ('undefined' != typeof jQuery)

$.data(img_obj, 'loaded', ('error'==e.type)?false:true);

if (settings.each instanceof Function) { settings.each.call(img_obj); }
if (settings.each instanceof Function) {
settings.each.call(img_obj);
}

if (deferred.notifyWith instanceof Function) { // support for jQuery < 1.7
deferred.notify(img_obj);
}

// http://jsperf.com/length-in-a-variable
if (loaded.length>=imgs.length && settings.all instanceof Function) { settings.all.call(loaded); }
if (loaded.length>=imgs.length) {
if (settings.all instanceof Function) {
settings.all.call(loaded);
}
deferred.resolve(loaded);
}

$(this).unbind('load error');
});

img.src = url;
});

return deferred.promise();
};

$.fn.imgpreload = function(settings)
Expand Down
4 changes: 1 addition & 3 deletions jquery.imgpreload.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions tests/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,15 @@
$.imgpreload( the_images, all_callback );
});

asyncTest('preload image array - promise resolved', function()
{
reset_image_urls();

$.imgpreload( the_images ).then(function(images) {
all_callback.call(images);
});
});

asyncTest('preload image array - { all: callback }', function()
{
reset_image_urls();
Expand Down