8

I have a script on my page that is dealing with session timeouts, redirecting the user on the client side when the session is due to expire. The complete code is somewhat more complex, but I have trimmed down the code to what is causing me the issue:

<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>

This code works as expected, redirecting after (roughly) ten seconds. However, if after the interval calls to CheckActivity have finished (after 5 seconds), I lock my screen and then unlock it after the redirect has due to have happened (another 5 seconds), the URL in my IE window has gone to 'test2.aspx', but the window seems to have frozen (still showing the first page).

This eventually un-freezes, but it takes 10s of seconds to get to the next page.

This only seems to happen in IE (IE9 on my machine), and is fine in chrome and firefox (and oddly IE6).

(Test2.aspx is a very simple page, only containing the text 'success'.)


Just noting that if I change the redirect from test.aspx to http://www.google.com/, this does not seem to be a problem. Still does not work however if I change the test2.aspx to be an absolute URL (the only main difference being that this would be a localhost address).

Paddy
  • 33,309
  • 15
  • 79
  • 114
  • Is the variable 'redirectTimeout' declared anywhere? Since its working oddly try clearing redirectTimeout. Just a shot in the dark ;D. – Matt Jul 20 '12 at 11:24
  • Sorry, redirectTimeout should have been in there. – Paddy Jul 20 '12 at 11:26
  • 1
    What about `window.location.replace("Test2.aspx");` or `window.navigate("Test2.aspx");` Or `self.location=Test2.aspx` have you tried these and do these also freeze? – 03Usr Jul 20 '12 at 11:33
  • @03Usr - none of these three made a difference to the result. – Paddy Jul 20 '12 at 13:02
  • Since another url is not crashing the browser, maybe the problem is in `test.aspx`? Does the browser also crash if you use a regular anchor or a button to trigger a function which sets `window.location`? – Torsten Walter Jul 24 '12 at 10:04
  • @TorstenWalter - These two pages were specially set up to get to the root of this problem and have not really got anything else in them bar this code. If I don't lock my screen, the redirect works as expected - it's the lock/unlock that is causing the issue. – Paddy Jul 24 '12 at 11:09
  • Sorry, I have misread that part. I have no IE at hand to test this, but maybe you could check if the browser window looses focus and stop the timer. Then when the browser regains focus, you could check if the session is over. [Detect browser window focus](http://www.thefutureoftheweb.com/blog/detect-browser-window-focus) – Torsten Walter Jul 24 '12 at 12:09
  • @TorstenWalter - please see answer and comments from Kjartan - it does seem to have performed the redirect, just frozen at this point. – Paddy Jul 24 '12 at 14:33
  • 1
    @Paddy - I am experiencing this same issue now in IE10. Did you end up finding a solution? The marked answer below does not work for me. It appears that the JavaScript events are still executing on time while the computer is locked.... however the window is just not repainted yet. Another thing I discovered is that if you click really fast about 3-5 times with your mouse on the frozen screen, it will repaint almost right away.... instead of having to wait 10 seconds. For me, it really has already redirected to new URL.... just has not repainted the screen to show this. – Jason Parker Mar 04 '14 at 21:46

3 Answers3

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.

Community
  • 1
  • 1
Jason Parker
  • 4,960
  • 4
  • 41
  • 52
1

Sounds as if IE is going into some form of "pause"-mode when you lock the screen, to avoid using cycles on rendering etc. Maybe you can try linking the redirection to the onFocus-event of the window, like this:

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

In theory, this should redirect the page as soon as focus is regained (ie when you unlock the screen). If the window already has focus, then this should not make any difference anyway.

Kjartan
  • 18,591
  • 15
  • 71
  • 96
  • Have considered doing something like this, but would still like to get to the bottom of why this is happening in the first place. Want to be sure I'm not missing something nefarious. – Paddy Jul 24 '12 at 13:25
  • After some testing, I'm pretty sure that this is just IE not performing the rendering; if you open the page, then click on another window (so IE looses focus), then lock the screen, and unlock it after 10+ seconds, IE will still be frozen on the number of seconds it was displaying when you locked the screen. The url however, will be updated. Focusing on the IE window again, will update the view, and take you to the redirected page. In other words, it seems as if you HAVE been redirected, but it is just not visible yet. – Kjartan Jul 24 '12 at 14:13
  • Indeed - you can see the new URL in the browser bar, but it just hasn't rendered yet, and it takes a long time to get its arse in gear. – Paddy Jul 24 '12 at 14:32
1

I don't have IE here so I can't verify, but maybe you can postpone the call to TimeoutRedirect until after the window is active again.

I've built a sample page to illustrate this.

The principle is rather simple. You while you run the interval and timeout, you;re setting a variable if the window becomes inactive, either because the user switches the tab or locks the screen. Once the timeout is up, you check if the window has focus. If not, set another variable to tell the focus handler to run your session end function. This should help with the redirect.

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.

Torsten Walter
  • 5,614
  • 23
  • 26
  • Indeed this is another way to do it, and have looked at something similar myself. It does however meant that the window will continue to appear 'open' until clicked, when it does the redirect, which is not ideal. – Paddy Jul 31 '12 at 13:28
  • Thanks for the example - just got a chance to look at it - seems to work. Will take a look at adapting my code. Still a bit curious as the the 'why' this was happening, but hey. – Paddy Jul 31 '12 at 15:55
  • I have no idea about the why, so I better not speculate. For the problem that the page looks active while in the background, you can still show some warning about the redirect or a semi transparent layer to visualize the session timeout while the window is not in focus. As long as no redirect occurs, you should be fine performance wise. – Torsten Walter Jul 31 '12 at 21:14
  • @Torsten Walter - I tested your sample page in IE10, but it's not working. As soon as I lock the screen, the count down being rendered on the page freezes. Steps: wait for countdown to start, lock screen, wait 20 seconds, unlock screen. Window has not repainted.... most recent message still says "15 seconds left" .... then I mouse click really fast about 3-5 times on the screen. This causes the screen to un-freeze and the rest of the "X Seconds Left" messages are immediately shown all at once. It seems one click won't do it... has to be a bunch of mouse clicks in quick succession. – Jason Parker Mar 04 '14 at 21:51
  • It seems the api has changed. In IE 10 it is now possible to use the [page visibility api](https://developer.mozilla.org/en-US/docs/Web/Guide/User_experience/Using_the_Page_Visibility_API). The example would have to be updated. – Torsten Walter Mar 24 '14 at 10:18