jQuery ajaxStop in ajax success
I have a jQuery plugin that i need to implement ajaxStop to a part of it. Below is a snippet from my code to show the structure (may not be 100%).
I have shown how my functions are setup as methods in case ajaxStop needs to be implemented as a global function, not sure how I would do that in the current setup.
The problem I'm having is occasionally ajaxStop doesn't get fired even if my ajax response comes back successfully.
- Can I call ajaxStop from an ajax success callback?
- What element should I call ajaxStop on $().ajaxStop OR $(this).ajaxStop
Thanks very much for any help!
Also perhaps anyone has had trouble with ajaxStop on a Mac? I just cannot find a solution to why this doesn't work all the time.
I think ajaxStop is not very reliable :(
(function($) {
$.fn.doRentalButtons = function($params) {
var newMethods = {
startAction:function() {
var $button = $('a.button');
开发者_JS百科 $button.click(function(){
var params = {one:'something',two:'morethings'}
$(this).doAjax(params);
});
},
doAjax:function(params) {
$current_button = $(this);
$container_div = $('#container');
$.ajax({
type: 'POST',
url: 'whatever.html',
data: params,
success: function(data) {
//dostuff
var jQueryObject =
if (global_var_hide == 0) {
$(this).afterPostRequests($current_button);
}
}()
});
},
afterPostRequests:function($current_button) {
$().ajaxStop(function(){
$(this).show();
$(this).positionDiv($current_button);
});
}
};
$.each(newMethods, function(i) {
$.fn[i] = this;
});
};
})(jQuery);
Just an update on this:
I ended up using a counter instead of AjaxStop. Increment the counter on each button click. And on ajax success / error minus 1 off the counter.
Then I only run my ajax request if the counter == 0.
This seems to work for what I need it for. But this does seem like a temporary solution and there is probably a better way.
(function($) {
$.fn.doRentalButtons = function($params) {
var ajax_busy = 0;
var newMethods = {
startAction:function() {
var $button = $('a.button');
$button.click(function(){
var params = {one:'something',two:'morethings'}
$(this).doAjax(params);
});
},
doAjax:function(params) {
$current_button = $(this);
$container_div = $('#container');
ajax_busy++;
$.ajax({
type: 'POST',
url: 'whatever.html',
data: params,
success: function(data) {
//dostuff
ajax_busy--;
var jQueryObject = $('#myDiv');
if (ajax_busy == 0) {
jQueryObject.show();
jQueryObject.positionDiv($current_button);
}
},
error: function() {
ajax_busy--;
}
});
}
};
$.each(newMethods, function(i) {
$.fn[i] = this;
});
};
})(jQuery);
精彩评论