最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery abort ajax call from another event - Stack Overflow

programmeradmin3浏览0评论

I have two click events, one of them has an ajax call, for the sake of clairty this could be click event A. I have another click event, let call this click event B.

I noticed that if I keep clicking on A multiple times rapidly and then click on B the events gets queued up, and even after event B has carried out, the ajax from A will get called.

When I click on B I want all ajax calls to stop altogether that are carried out from A.

Inside click event A I have:

$("A").live("click", function(event) {
    var ajax_call = $.ajax({ /* Some code goes here */ });
});

$("B").live("click", function(event) {
    /* Some code goes here */
   ajax_call.abort();
});

But the above doesn't work. Is there a way where I can clear the backlog of events carried out from A?

I have two click events, one of them has an ajax call, for the sake of clairty this could be click event A. I have another click event, let call this click event B.

I noticed that if I keep clicking on A multiple times rapidly and then click on B the events gets queued up, and even after event B has carried out, the ajax from A will get called.

When I click on B I want all ajax calls to stop altogether that are carried out from A.

Inside click event A I have:

$("A").live("click", function(event) {
    var ajax_call = $.ajax({ /* Some code goes here */ });
});

$("B").live("click", function(event) {
    /* Some code goes here */
   ajax_call.abort();
});

But the above doesn't work. Is there a way where I can clear the backlog of events carried out from A?

Share Improve this question asked Sep 9, 2011 at 15:09 lemonlemon 6051 gold badge8 silver badges21 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

The problem you are facing is probably related to the concept called closure. The variable from the first callback is not accessible from the second callback. The solution may be the following:

var ajax_call;

$("A").live("click", function(event) {
    ajax_call = $.ajax({ /* Some code goes here */ });
});

$("B").live("click", function(event) {
    /* Some code goes here */
    ajax_call.abort();
});

Does it work for you?

Seems like a scope issue. ajax_call should be accesible for both events:


var ajax_call;

$("A").live("click", function(event) {
    ajax_call = $.ajax({ /* Some code goes here */ });
});

$("B").live("click", function(event) {
    /* Some code goes here */
   ajax_call.abort();
});

发布评论

评论列表(0)

  1. 暂无评论