2

我从这里使用 JQuery idleTimeout 插件:

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

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

请建议!

4

1 回答 1

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");
        }

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

感谢大家调查这个问题!

于 2013-10-23T21:53:36.880 回答