Yovo
Yovo

Reputation: 629

jquery same ajax call kill

I have ajax request

  result = $.ajax({
              url: '/live-sell-search.php?id=123',
              success: function(result) {
              $('.results-list').html(result);
                }
            });

I call this on change and I am trying to kill previous call if it is in progress with somthing like this

if(result!='undefined'){resul.abort();}

and several variations of this but it is always braking at some point. Can anyone tell me how this can be done?

Upvotes: 1

Views: 500

Answers (1)

Tomgrohl
Tomgrohl

Reputation: 1767

As other people have stated, you need to start accepting answers.

Updated

I use this on a website and it works quite well for me.

When the onChange event is called, it checks if there is already an AJAX call in affect. If so, it aborts it and starts a new AJAX call.

Once the AJAX call has completed the xhr variable gets nulled. Simple really.

var xhr = null;

$("#textbox").change(function(){

    if( xhr != null ) {
         xhr.abort();
    }

    xhr = $.ajax({
              url: '/live-sell-search.php?id=123',
              complete: function( xhr ){
                  xhr = null; // empty xhr object
              },
              success: function( data ) {
                  $('.results-list').html( data );
              }
          });
});

Upvotes: 4

Related Questions