在JQuery idleTimeout功能中出现“pollingInterval:”问题

时间:2013-10-10 21:09:11

标签: javascript ajax jquery session-timeout

我从这里使用JQuery idleTimeout插件:

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

当我在我的代码中添加它时,它运行正常,但每当我启动页面每次刷新5次时,我通过删除idleTimeout代码进行检查。然后它没有给出这个问题。

在详细介绍之后,我发现如果删除或评论“pollingInterval:2”代码行,一切正常,刷新问题就会解决,TimeOut功能仍然有效。

有人可以帮助我了解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

如果您需要任何其他信息,请与我们联系!

请建议!

1 个答案:

答案 0 :(得分:2)

我能找到解决问题的方法,所以想分享它:

问题在于我错过了一行代码: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”控制器下我创建了一个返回“OK”的ContentClass:

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

它有效,我的页面停止刷新!

感谢大家一直在研究这个问题!