I have some ajax calls on the document of a site that display or hide a progress bar depending on the ajax status
$(document).ajaxStart(function(){ $('#ajaxProgress').show(); }); $(document).ajaxStop(function(){ $('#ajaxProgress').hide(); });
I would like to basically overwirte these methods on other parts of the site where a lot of quick small ajax calls are made and do not need the progress bar popping in and out. I am trying to attach them to or insert them in other $.getJSON and $.ajax calls. I have tried chaining them but apparently that is no good.
$.getJSON().ajaxStart(function(){ 'kill preloader'});
The ajaxComplete() method specifies a function to be run when an AJAX request completes. Note: As of jQuery version 1.8, this method should only be attached to document. Unlike ajaxSuccess(), functions specified with the ajaxComplete() method will run when the request is completed, even it is not successful.
1. Using setInterval() It repeatedly calls the function on the given interval for stop you need to clear the interval using clearInterval() or close the window. Creating a function that calls the AJAX request and using this function in setInterval() and set Interval for 5 sec.
context: It is used to specify the “this” value for all AJAX-related callback functions. data: It is used to specify data to be sent to the server. dataFilter(data, type): It is used to handle the raw response data of the XMLHttpRequest.
2018 NOTE: This answer is obsolete; feel free to propose an edit to this answer that will work.
You can bind the ajaxStart and ajaxStop using custom namespace:
$(document).bind("ajaxStart.mine", function() { $('#ajaxProgress').show(); }); $(document).bind("ajaxStop.mine", function() { $('#ajaxProgress').hide(); });
Then, in other parts of the site you'll be temporarily unbinding them before your .json calls:
$(document).unbind(".mine");
Got the idea from here while searching for an answer.
EDIT: I haven't had time to test it, alas.
There is an attribute in the options object .ajax() takes called global.
If set to false, it will not trigger the ajaxStart event for the call.
$.ajax({ url: "google.com", type: "GET", dataType: "json", cache: false, global: false, success: function (data) {
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With