jQuery Ajax 事件绑定

jQuery Ajax event binding

本文关键字:绑定 事件 Ajax jQuery      更新时间:2023-09-26

我正在使用 https://github.com/ehynds/jquery-idle-timeout 来生成一个Mint风格的空闲计时器,该计时器对"keep-alive"页面执行Ajax调用。

我还有一段 Javascript for Ajax 表单提交,它会弹出一条"请稍候"消息,提醒用户尽管页面未加载,但活动正在发生。

出于某种原因,每次轮询保持活动状态页面时,toggleAjaxLoader() 函数都会绑定到 ajax:before 和 ajax:complete 事件。 我不希望这样,因为这会让用户感到困惑。为什么这会绑定到空闲超时和/或我如何深入了解正在发生的事情?

加载动画:

// Toggles our animated ajax loader image
function toggleAjaxLoader() {
  jQuery('#ajax_loader').toggle();
}

空闲超时:

/*
 * Inactivity notifier and auto logout
 */
jQuery(function(){
    var redirectToURL = getAbsoluteUrl('/logout/auto=true'); // URL to relocate the user to once they have timed out
    var keepAlive = getAbsoluteUrl('/keep-alive');
    if (jQuery("#idletimeout").length) {
        $.idleTimeout('#idletimeout', '#idletimeout a', {
            idleAfter: 2700, // 45 minutes
            warningLength: 60, // number of seconds to wait before redirecting the user
            keepAliveURL: keepAlive,
            AJAXTimeout: 2500,
            pollingInterval: 5, // 60
            expiredMessage: 'Your session has expired.  You are being logged out for security reasons.', // message to show user when the countdown reaches 0
            onTimeout: function(){
                $(this).slideUp();
                window.location.replace(redirectToURL);
            },
            onIdle: function(){
                $(this).slideDown(); // show the warning bar
            },
            onCountdown: function( counter ){
                $(this).find("span").html( counter ); // update the counter
            },
            onResume: function(){
                $(this).slideUp(); // hide the warning bar
                // Tums.bump_tums_session(session[:user].session['sessionGuid']);
            }
        });
    };
});
我相信

我找到了答案,这是由于jQuery的全局ajax事件处理程序,默认情况下该设置设置为true。 我将其设置为 false,它似乎按预期工作。

$.ajaxSetup({
   global: false
});