Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call .ajaxStart() on specific ajax calls

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'}); 
like image 805
kevzettler Avatar asked Jul 28 '09 01:07

kevzettler


People also ask

How would you fire a callback when any AJAX request on a page has completed?

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.

How do you fire AJAX request on regular interval?

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.

Which three parameters are used in the sequence of AJAX call?

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.


2 Answers

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.

like image 131
montrealist Avatar answered Sep 28 '22 11:09

montrealist


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) { 
like image 44
Hasnat Safder Avatar answered Sep 28 '22 11:09

Hasnat Safder