How to cancel a jquery.load()?
Posted
by Dirk Jönsson
on Stack Overflow
See other posts from Stack Overflow
or by Dirk Jönsson
Published on 2010-05-11T17:58:24Z
Indexed on
2010/05/11
19:34 UTC
Read the original article
Hit count: 253
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 have is...
...the timeout handling:
jQuery.fn.idle = function(time, postFunction){
var i = $(this);
i.queue(function(){
setTimeout(function(){
i.dequeue();
postFunction();
}, time);
});
return $(this);
};
... initializing of the error message timeout:
var hasImage = false;
$('#errorMessage')
.idle(5000, function() {
if(!hasImage) {
// 1. cancel .load()
// 2. show error message
}
});
... the image loading:
$('#myImage')
.attr('src', '/url/anypath/image.png')
.load(function(){
hasImage = true;
// do something...
});
The only thing I could not figure out is how to cancel the running load() (if it's possible).
Please help. Thanks!
Edit:
Another way: How do I prevent the .load() method to call it's callback function when it's returning?
© Stack Overflow or respective owner