I\'d like to cancel a .load() operation, when the load() does not return in 5 seconds. If it\'s so I show an error message like \'sorry, no picture loaded\'.
What I
You could simply set a timeout before loading the image as well as testing for a load error.
function LoadImage(yourImage) {
var imageTimer = setTimeout(function () {
//image could not be loaded:
alert('image load timed out');
}, 10000); //10 seconds
$(yourImage).load(function (response, status, xhr) {
if (imageTimer) {
if (status == 'error') {
//image could not be loaded:
alert('failed to load image');
} else {
//image was loaded:
clearTimeout(imageTimer);
//display your image...
}
}
});
}