Detect when an image has finished loading

var img = new Image();

img.onload = function () {
    $('#preview').attr('src', img.src).removeClass('is-loading');
};

// onload has to be assigned before src, or a cached image never fires it
img.src = '/uploads/product-5512-large.jpg';