Disable ajaxStart() and ajaxStop() for a specific request

Gung Foo picture Gung Foo · Sep 26, 2012 · Viewed 23.7k times · Source

I am using .ajaxStart() and .ajaxStop() to show a modal while an ajax request is being made. (between start and stop)

Now I'd like to add a longpoll function that keeps waiting for notifications, similar to the one on the left upper corner of this site.

My problem now lies in disabling this modal only for the longpolling request..

Registering "loading screen" on and off handlers:

$(document).ajaxStart(handleAjaxStart);
$(document).ajaxStop(handleAjaxStop);

My longpoll function:

$.ajax({
    timeout: 35000,
    url: longPollUrl,
    success: function(data){
        if(data.queCount) $('#numQueCount').html(data.queCount);
        if(data.queAccept) $('#numQueAccept').html(data.queAccept);
    }, 
    dataType: 'json',
    complete: longpoll
});

I tried:

$().off('ajaxStart');
$().off('ajaxStop');

..and reattaching the handlers after starting the polling, but no joy.

I also tried introducing a global variable into handleAjaxStart() that would return at the first line of the function, but that seems to completely kill the loading screen.

Any ideas how this can be achieved?

Answer

Gung Foo picture Gung Foo · Sep 27, 2012

I figured it out..

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({
    timeout: 35000,
    url: longPollUrl,
    success: function(data){
        if(data.queCount) $('#numQueCount').html(data.queCount);
        if(data.queAccept) $('#numQueAccept').html(data.queAccept);
    }, 
    global: false,     // this makes sure ajaxStart is not triggered
    dataType: 'json',
    complete: longpoll
});