8

我的页面上有一个脚本处理会话超时,当会话到期时在客户端重定向用户。完整的代码稍微复杂一些,但我已将代码精简为导致我出现问题的原因:

<head runat="server">
    <script src="javascript/jquery-1.7.2.min.js" type="text/javascript">
    </script>
    <script type="text/javascript">

        /*

            Please see document ready method at the bottom that sets
            up this code, calling CheckActivity() at intervals.

        */

        var warningInterval, redirectTimeout;
        var now = 1;

        function TimeoutRedirect() {
            window.location.href = "Test2.aspx";
        }

        //In this example this is a bit of a null op, but in real
        //code this will display a warning a minute or so prior to redirect.
        //This is required to recreate...
        function CheckActivity() {
            if (now > 4) {
                clearInterval(warningInterval);

                redirectTimeout = setTimeout(function () { 
                         TimeoutRedirect(); }, 5000);
            }

            //Some visual activity for testing purposes.
            $("#CheckActivityCount").text(now);
            now++;
        }


        $(document).ready(function () {
            warningInterval = setInterval(function () { 
                CheckActivity(); }, 1000);
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div id="CheckActivityCount">

        </div>
    </div>
    </form>
</body>

此代码按预期工作,在(大约)十秒后重定向。但是,如果对 CheckActivity 的间隔调用完成后(5 秒后),我锁定我的屏幕,然后在重定向发生后解锁它(再过 5 秒),我的 IE 窗口中的 URL 已转到“test2” .aspx',但窗口似乎已冻结(仍显示第一页)。

这最终会解冻,但需要 10 秒才能到达下一页。

这似乎只发生在 IE(我的机器上的 IE9)中,并且在 chrome 和 firefox(以及奇怪的 IE6)中很好。

(Test2.aspx 是一个非常简单的页面,只包含文本'success'。)


请注意,如果我将重定向从 test.aspx 更改为http://www.google.com/,这似乎不是问题。但是,如果我将 test2.aspx 更改为绝对 URL(唯一的主要区别是这将是 localhost 地址),仍然不起作用。

4

3 回答 3

4

I was running into the same type of issue, and I created a different question so I could focus in on what I perceived to be the heart of the issue. (IE not repainting while computer is in lock screen.)

Anyway, I finally figured out a way to resolve this. You can see my answer here. Essentially you can fix your issue by putting some JavaScript inside your auto-logout destination page to periodically update the page content.... and this will force IE to repaint the page.

This way I can execute any Auto Saving logic I want right before their session expires on their current page, and then kick them to the Auto-Logout page.

于 2014-05-02T17:24:38.367 回答
1

听起来好像 IE 在您锁定屏幕时进入某种形式的“暂停”模式,以避免在渲染等时使用循环。也许您可以尝试将重定向链接到窗口的onFocus事件,如下所示:

window.onfocus = function(){
    window.location.href = "Test2.aspx";
}
window.location.href = "Test2.aspx";

理论上,一旦重新获得焦点(即解锁屏幕时),这应该重定向页面。如果窗口已经有了焦点,那么无论如何这应该没有任何区别。

于 2012-07-24T13:03:05.020 回答
1

我这里没有 IE,所以我无法验证,但也许您可以推迟对 TimeoutRedirect 的调用,直到窗口再次处于活动状态。

我已经建立了一个示例页面来说明这一点。

原理比较简单。您在运行间隔和超时时,如果窗口变为非活动状态,您将设置一个变量,因为用户切换选项卡或锁定屏幕。一旦超时,您检查窗口是否有焦点。如果没有,请设置另一个变量来告诉focus处理程序运行您的会话结束函数。这应该有助于重定向。

sessionEnd: function() {
    this.sessionEnded = true;
    var windowEvents;
    if (!this.isInactive) {
        console.log("window active, sessionEnd called");
        alert("THE END");
        this.removeEvents();
        // window.location.href = "test.aspx";
    } else {
        console.log("window inactive, sessionEnd will be called again on focus");
    }

},

handleEvent: function(e) {
    // only do something if the window loses or gains focus
    if (this.eventBlurRegex.test(e.type)) {
        this.isInactive = true;
    } else if (this.eventFocusRegex.test(e.type)) {
        this.isInactive = false;
        if (this.sessionEnded === true) {
            this.sessionEnd.call(this);
        }
    }

},

For older IE versions which don't support the handleEvent function I've used the polyfill found at CSS NInja and modified it a little bit.

I hope this helps.

于 2012-07-24T19:38:08.903 回答