Skip to content

Instantly share code, notes, and snippets.

@Rycochet
Created February 17, 2014 09:32
Show Gist options
  • Save Rycochet/9047524 to your computer and use it in GitHub Desktop.
Save Rycochet/9047524 to your computer and use it in GitHub Desktop.
Perform a callback when an image has loaded, optionally on every load
/**
* Perform a callback when an image has loaded, optionally on every load
* @param {function} callback
* @param {?boolean} repeat
* @returns {jQuerySelector}
*/
$.fn.imageLoad = function(callback, repeat) {
return this.each(function() {
var $el = $(this), fn = function() {
callback.call(this, $el);
};
if (this.complete) {
fn();
} else {
$el[repeat ? "on" : "one"]("load", fn);
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment