Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cancel a jquery.load()?

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).

Edit:

Another way: How do I prevent the .load() method to call it's callback function when it's returning?

like image 882
Dirk Avatar asked May 11 '10 17:05

Dirk


People also ask

How to stop jQuery function?

jQuery stop() Method The stop() method works for all jQuery effect functions, including sliding, fading and custom animations. Syntax: $(selector). stop(stopAll,goToEnd);

How to stop toggle in jQuery?

Toggling Animations As of jQuery 1.7, stopping a toggled animation prematurely with . stop() will trigger jQuery's internal effects tracking. In previous versions, calling the . stop() method before a toggled animation was completed would cause the animation to lose track of its state (if jumpToEnd was false).

What is jQuery load method?

The jQuery load() method is a simple, but powerful AJAX method. The load() method loads data from a server and puts the returned data into the selected element. Syntax: $(selector).load(URL,data,callback); The required URL parameter specifies the URL you wish to load.

How to use load jQuery?

The Load() method in jQuery helps to load data from server and returned into selected element without loading the whole page. Syntax: $(selector). load(URL, data, callback);


2 Answers

If you want any custom handling such as this, you simply can't use the jQuery.load() function. You'll have to upgrade to jQuery.ajax(), which I recommend anyway since you can do so much more with it, especially if you need any kind of error handling, it will be necessary.

Use the beforeSend option for jQuery.ajax and capture the xhr. Then you can create callback which can cancel the xhr after your timeout, and the callbacks as necessary.

This code is not tested, but should get you started.

var enableCallbacks = true;
var timeout = null;
jQuery.ajax({
  ....
  beforeSend: function(xhr) {
    timeout = setTimeout(function() {
      xhr.abort();
      enableCallbacks = false;
      // Handle the timeout
      ...
    }, 5000);
  },
  error: function(xhr, textStatus, errorThrown) {
    clearTimeout(timeout);
    if (!enableCallbacks) return;
    // Handle other (non-timeout) errors
  },
  success: function(data, textStatus) {
    clearTimeout(timeout);
    if (!enableCallbacks) return;
    // Handle the result
    ...
  }
});
like image 158
aceofspades Avatar answered Sep 21 '22 17:09

aceofspades


Your rephrased secondary question:
How do I cancel pending callback functions, created using the .load() method?

You can cancel all jquery callbacks, by using this 'nuclear' option:

$('#myImage').unbind('load');
like image 44
Carver Avatar answered Sep 23 '22 17:09

Carver