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.
我有两个点击事件,其中一个有一个ajax调用,为了clairty,这可能是点击事件A.我有另一个点击事件,让我们调用这个点击事件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.
我注意到如果我多次快速点击A然后点击B,事件就会排队,即使事件B执行完毕,A的ajax也会被调用。
When I click on B I want all ajax calls to stop altogether that are carried out from A.
当我点击B时,我想要从A执行的所有ajax调用完全停止。
Inside click event A I have:
内部点击事件A我有:
$("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?
但上述方法无效。有没有办法可以清除A中积压的事件?
2 个解决方案
#1
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?
对你起作用吗?
#2
3
Seems like a scope issue. ajax_call
should be accesible for both events:
似乎是一个范围问题。两个事件都应该可以访问ajax_call:
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();
});
#1
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?
对你起作用吗?
#2
3
Seems like a scope issue. ajax_call
should be accesible for both events:
似乎是一个范围问题。两个事件都应该可以访问ajax_call:
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();
});