重置ajax post之间的idleTimeout计数器

Reset idleTimeout counter between ajax post

本文关键字:idleTimeout 计数器 之间 ajax post 重置      更新时间:2023-09-26

我在这里使用JQuery idleTimeout插件:

http://www.erichynds.com/examples/jquery-idle-timeout/example-mint.htm

我在mvc 4应用程序中使用它。

下面是我设置会话计时器的代码片段。

 <script type="text/javascript">
  var sessionTimer = 60;
  $(document).ready(function ()
  {
    // setup the dialog
    $("#dialog").dialog({
        autoOpen: false,
        modal: true,
        width: 400,
        height: 210,
        closeOnEscape: false,
        draggable: false,
        resizable: false,
        buttons: {
            'Yes, Keep Working': function () {
                $(this).dialog('close');
            },
            'No, Logoff': function () {
                // fire whatever the configured onTimeout callback is.
                // using .call(this) keeps the default behavior of "this" being the warning
                // element (the dialog in this case) inside the callback.
                $.idleTimeout.options.onTimeout.call(this);
            }
        }
    });

        var $countdown = $("#dialog-countdown");
        @* start the idle timer plugin *@
        $.idleTimeout('#dialog', '#dialog-button-yes', {
            idleAfter: (sessionTimer - 30),
            keepAliveURL: '@Url.Action("KeepAlive", "Home")',
            pollingInterval: 5,
            serverResponseEquals: 'OK',
            AJAXTimeout: 250 * 60,
            onTimeout: function () {
                window.location = '@Url.Action("Logout", "Login")';
        },
        onIdle: function () {
            $(this).dialog("open");
        },
        onCountdown: function (counter) {
            $countdown.html(counter); @* update the counter *@
        }
    });
  });

这段代码位于最外层/共享视图中。我所有的页面都是使用jquery $.ajax的部分视图加载的。上面的代码只加载一次,sessionTimer被设置为60秒。因此,当调用ajax post加载新页面时,计时器不会重置。即使用户处于活动状态,计时器也会在帖子之间滴答作响。是否有一种方法让我重置计数器每次ajax帖子发生。我可以在每个inner views $上重置这个。Ajax成功条件。但是有太多的地方。我想知道是否有一个共同的代码,我可以在我的这个主页上写,这将让我知道一个ajax调用已经放置,并重置计数器。

尝试使用来自jQuery的.ajaxSuccess()事件处理程序。您可以在这里查看如何使用它的文档:http://api.jquery.com/ajaxsuccess/.