user1123379
user1123379

Reputation: 334

jquery how to stop $.ajax when you click on a button

How can I stop him in the $.ajax() function when you click on this button:

<button class="stop">Stop</button>

There is any function that cause to $.ajax() stop?

Note: My $.ajax script is found in a function, for example:

function useajax()
{
    $.ajax(...)
}

Upvotes: 1

Views: 3406

Answers (1)

jrummell
jrummell

Reputation: 43077

$.ajax() returns a jqXHR, which has an abort method.

var jqXHR;
function useajax()
{
    jqXHR = $.ajax(url, data, function() { /* complete */});
}

$(".stop").click(function(){ jqXHR.abort(); });

Upvotes: 3

Related Questions