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

现在我想添加一个longpoll函数,它会一直等待通知,类似于这个网站左上角的那个.

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

注册"加载屏幕"打开和关闭处理程序:

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

我的LongPoll函数:

$.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?

推荐答案

我想出来了..

There is an attribute in the options object .ajax() takes called global.

如果设置为false,则不会触发该调用的ajaxStart事件.

$.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
});

Jquery相关问答推荐

在shiny 的datatable列中启用智能搜索

Django BaseDataTableView-Filter_queryset方法不起作用

将选定2个AJAX结果显示到模板中

为什么 .join() 不能与函数参数一起使用?

使用 jQuery / javascript 测试链接是否是外部的?

如何获取将在不提交的情况下提交的所有表单值

$(document).ready(function() VS $(function(){

如何使用 jQuery 禁用粘贴(Ctrl+V)?

提交前添加 POST 参数

jquery如何捕获输入键并将事件更改为选项卡

将输入更改为大写

Ajax 更新后在 jQuery 中重新绑定事件(更新面板)

jQuery .hide() 和 .css("display", "none") 的区别

如何使用jQuery动态设置宽度和高度

用jquery替换锚文本

如何使用 jQuery 或纯 JS 重置所有复选框?

如何将键和值都推送到 Jquery 中的数组中

由于滚动,响应表内的 bootstrap 按钮下拉菜单不可见

Jquery Select 器输入[type=text]')

清除表jquery