当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - abort the right ajax

问题描述:

My question is probably nooby but I really cannot find an answer actually.

I want to use abort() method on a specific ajax. However i always use request=$.ajax...for all my requests and the request.abort() cancell ALL my ajax, intead of only the one i want.

Is there a way to point on the right one by naming it or something?

here is my code

request.abort();

request = $.ajax({

url: "getphp/gettooltip.php",

type: "GET",

data: {db : db, id : url.substring(url.lastIndexOf('=')+1)},

dataType: "JSON"

});

request.done(function(msg){

d3lttFillInTooltip(msg,db)

$('#d3ltttooltipdiv').css('visibility','visible');

});

I absolutely need to cancel the last call of this same ajax before running this one.

Any help would be welcome :)

网友答案:

You need to change your code so that you are not simply assigning request=$.ajax({...}); for every single call. You need some sort of list or mapping of requests. How you implement this depends on when you need to abort requests. For example, if you just wanted to have a stack of requests, so that you could easily abort the last request, you could do something like this:

var requests = [];
requests.push($.ajax({
    // request 1
    ...
}));
requests.push($.ajax({
    // request 2
    ...
}));
requests.push($.ajax({
    // request 3
    ...
}));

requests.pop().abort(); //aborts request 3
// or...
requests.shift().abort(); //aborts request 1

If this doesn't help you, please provide more info on when you need to abort requests. Bottom line -- don't set request to every single ajax request you make if you want to be able to target specific requests.

网友答案:

Use a different variable for each jqXHR object.

分享给朋友:
您可能感兴趣的文章:
随机阅读: