Add the imgPath variable to store URL of the image, after loading the function will work add the functionality in the function you want to add like adding image or removing any image etc. So on before we can add loading gif or something on UI to show the loading state
$('<img src="'+ imgPath +'">').load(function() {
$(this).width(some).height(some).appendTo('#some_target');
});
Or use this method
var img = new Image();
img.onload = function () {
if (!_.isEmpty(self.imageurl)) {
$('.product-details-image-gallery').removeClass("showloading")
$('.product-details-image-gallery-detailed-image').find('img').attr('src', self.imageurl);
$('.product-details-image-gallery-container').find('img').attr('src', self.imageurl);
}
}
img.src = previwImgUrl;