javascript - JQueryidleTimeout 功能中的 "pollingInterval:"问题

标签 javascript ajax jquery session-timeout

我在这里使用 JQueryidleTimeout 插件:

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

当我在代码中实现时它工作得很好,但是每当我启动页面时它每次都会刷新5次,所以我通过删除idleTimeout代码进行检查。然后它就没有给出这个问题。

详细了解后,我发现如果删除或注释“pollingInterval:2”代码行,一切正常,刷新问题得到解决,并且超时功能仍然有效。

有人可以帮助我了解 pollingInterval:2 实际上在做什么吗?

这是插件的代码

JS:

 <script type="text/javascript">
        // 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);
                }
            }
        });

        // cache a reference to the countdown element so we don't have to query the DOM for it on each ping.
        var $countdown = $("#dialog-countdown");

        // start the idle timer plugin
        $.idleTimeout('#dialog', 'div.ui-dialog-buttonpane button:first', {
            idleAfter: 600,
            pollingInterval: 2, //<--- THIS IS CAUSING ISSUE
            serverResponseEquals: 'OK',
            onTimeout: function () {
                window.location = "/Home/Index/";
            },
            onIdle: function () {
                $(this).dialog("open");
            },
            onCountdown: function (counter) {
                $countdown.html(counter); // update the counter
            }
        });

</script>

它生成的对话框,

<!-- Session Time Out Message box -->
<div id="dialog" title="Your session is about to expire!">
    <p><span class="ui-icon ui-icon-alert" style="float: left; margin: 0 7px 50px 0;"></span>You will be logged off in <span id="dialog-countdown" style="font-weight: bold"></span>seconds. </p>
    <p>Do you want to continue your session?</p>
</div>

这里是需要添加的2个Jquery:

http://www.erichynds.com/examples/jquery-idle-timeout/src/jquery.idletimer.js
http://www.erichynds.com/examples/jquery-idle-timeout/src/jquery.idletimeout.js

如果您需要任何其他信息,请告诉我!

请提出建议!

最佳答案

我找到了问题的解决方案,因此考虑分享:

问题是我缺少一行代码:keepAliveURL: '/Home/KeepAlive'

            $.idleTimeout('#dialog', 'div.ui-dialog-buttonpane button:first', {
                idleAfter: 600,
                keepAliveURL: '/Home/KeepAlive',
                pollingInterval: 2,
                serverResponseEquals: 'OK',
                onTimeout: function () {
                    window.location = "/Home/Index/";
                },
                onIdle: function () {
                    $(this).dialog("open");
                },
                onCountdown: function (counter) {
                    $countdown.html(counter); // update the counter
                }
            });

根据这一行,我返回一条消息“OK”,因为“serverResponseEquals:”正在寻找响应“OK”。我的代码位于 MVC 中,因此在“Home” Controller 下我创建了一个返回“OK”的 ContentClass:

   public ContentResult KeepAlive()
        {
            return Content("OK");
        }

它成功了,我的页面停止刷新!

感谢大家调查此问题!

关于javascript - JQueryidleTimeout 功能中的 "pollingInterval:"问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19306262/

相关文章:

javascript - Firefox 插件中面板内容脚本和页面内容脚本之间的全局变量

javascript - php:为 highcharts 渲染 javascript 时,laravel 慢速 View 渲染时间

javascript - 将元素内容作为纯文本获取所有后代

javascript - 在javascript和PHP中的字符串中获取多个选中的复选框值

javascript 不在 IE8/9 中运行,但在查看控制台时运行

jquery - 在ajax表单提交来自ckeditor textarea的值之后,不通过post发送

php - 在选择下拉值时执行一个 php 文件

javascript - 如何让侧边栏向上移动

javascript - 查找最近的元素,而不是最近的元素

jquery - 获取 D3 中数组的实际最大值