Javascript setTimeout and redirect - IE freezes

后端 未结 3 624
礼貌的吻别
礼貌的吻别 2021-02-15 13:09

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

3条回答
  •  广开言路
    2021-02-15 13:34

    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.

提交回复
热议问题