SamesJeabrook
SamesJeabrook

Reputation: 1567

Cancelling an ajax request within an object

I have an ajax request written in an object, I need to write a function which cancels this specific request. I've looked at the abort method but not sure how to get it to work this way.

getItems: function(categoryID, format, numberOfResults, types, sortBy, searchString){
    var url = host+"getItems?categoryID="+categoryID+"&format="+format+"&numberOfResults="+numberOfResults+"&types="+types+"&sortBy="+sortBy+'&fileName='+searchString;
    return $.ajax({
        type:"get",
        dataType:"html",
        url: url,
        success:function(data){
            resItems = data;
        },
        error: function(data){
            console.log(data);
        }
    });
},

The problem I have is this function is called when certain elements are called, I need to cancel this request before initiating the next one if the user clicks on two elements in quick succession.

The page has multiple ajax requests so I can't do a blanket cancel all but need to cancel this one specifically.

The structure for the object is like this:

var items: {
    getItems: function(){
    },
    getItemInfo: function(){
    }
}

and so on.

What I would like to do is target an ajax request based on its request URL and cancel it, is that possible from an external function?

Upvotes: 0

Views: 49

Answers (1)

john Smith
john Smith

Reputation: 17906

because getItems is returning the XMLHttpRequest it should be as simple as

resp = items.getItems(params)
resp.abort()

Upvotes: 1

Related Questions