第一次用户扩展会话后,Jquery 超时代码不会重新运行

发布于 2024-09-17 19:19:27 字数 3311 浏览 4 评论 0原文

我编写了一个 Jquery 函数,该函数在一定时间不活动后会黑屏,创建一个弹出窗口,允许用户单击按钮以保持登录状态,如果用户没有响应,则将其注销(关闭应用程序窗口)及时。

环境是ASP.NET(VB)。从技术上讲,我们不使用母版页,但我们确实有一个父页面,其中包含页眉、页脚和导航栏,并且从该窗口调用我的 Jquery 代码,并通过 IFrame 加载。

我在子窗口中有一个函数,可以向父窗口报告活动(当前为 keydown、mousedown 和模糊),并重置计时器。我的代码似乎工作正常,除了一种情况之外。如果用户收到超时警告提示,然后单击按钮继续会话,如果他们在页面上未执行任何操作(鼠标单击、按键等),则超时代码不会再次运行。

这是我的主要 jquery 函数:

function pop_init() {
    // show modal div
    $("html").css("overflow", "hidden");
    $("body").append("<div id='popup_overlay'></div><div id='popup_window'></div>");
    //$("#popup_overlay").click(popup_remove);  // removed to make sure user clicks button to continue session.
    $("#popup_overlay").addClass("popup_overlayBG");
    $("#popup_overlay").fadeIn("slow");

    // build warning box
    $("#popup_window").append("<h1>Warning!!!</h1>");
    $("#popup_window").append("<p id='popup_message'><center>Your session is about to expire.  Please click the button below to continue working without losing your session.</center></p>");
    $("#popup_window").append("<div class='buttons'><center><button id='continue' class='positive' type='submit'><img src='images/green-checkmark.png' alt=''/> Continue Working</button></center></div>");

    // attach action to button
    $("#continue").click(session_refresh);

    // display warning window
    popup_position(400, 300);
    $("#popup_window").css({ display: "block" }); //for safari using css instead of show
    $("#continue").focus();
    $("#continue").blur();

    // set pop-up timeout
    SESSION_ALIVE = false;
    window.setTimeout(popup_expired, WARNING_TIME);
}

这是父窗口的代码:

<script language="javascript" type="text/javascript">
            var timer = false;
            window.reportChildActivity = function() {
                if (timer !== false) {
                    clearTimeout(timer);
                }
                //SESSION_ALIVE = true;
                timer = window.setTimeout(pop_init, SESSION_TIME);
            }
  </script>

这是子窗口的代码:

<script type="text/javascript">
    $(document).ready(function() {
        window.parent.reportChildActivity();
    });
</script>
<script type="text/javascript">
    $(document).bind("mousedown keydown blur", function() {        
            window.parent.reportChildActivity();
    });

最后一个脚本在一个文件(VB.NET ascx 文件)中运行,该文件为每个页面构建标题/菜单选项我们的系统。

pop_init 函数中的最后一行显然应该重新启动计时器,但由于某种原因它似乎不起作用。

感谢您提供的任何帮助和见解。

忘记为 session_refresh 函数添加我的代码:

function session_refresh() {
    SESSION_ALIVE = true;
    $(".buttons").hide();
    $("#popup_message").html("<center><br />Thank you!  You may now resume using the system.<br /></center>");
    window.setTimeout(popup_remove, 1000);
    $("#popup_window").fadeOut("slow", function() { $('#popup_window,#popup_overlay').trigger("unload").unbind().remove(); });
    var timer = false;
    window.reportChildActivity = function() {
        if (timer !== false) {
            clearTimeout(timer);
        }
        timer = window.setTimeout(pop_init, SESSION_TIME);
    }
}

I wrote a Jquery function that blacks out the screen after a certain amount of inactivity, creates a pop-up that allows the user to click a button to stay logged in, and logs them out (closing the application window) if they do not respond in time.

The environment is ASP.NET (VB). We don't technically use master pages, but we do have a parent page in which our header, footer and nav reside, and my Jquery code is called from that window, loaded via an IFrame.

I've got a function in the child window that reports activity (currently keydown, mousedown and blur) to the parent window, and resets the timer. My code seems to be working fine, except in one scenario. If the user is prompted with the timeout warning, and then they click the button to continue their session, if they take no action on the page (mouseclick, keydown, etc.) then the timeout code is not running a second time.

Here is my main jquery function:

function pop_init() {
    // show modal div
    $("html").css("overflow", "hidden");
    $("body").append("<div id='popup_overlay'></div><div id='popup_window'></div>");
    //$("#popup_overlay").click(popup_remove);  // removed to make sure user clicks button to continue session.
    $("#popup_overlay").addClass("popup_overlayBG");
    $("#popup_overlay").fadeIn("slow");

    // build warning box
    $("#popup_window").append("<h1>Warning!!!</h1>");
    $("#popup_window").append("<p id='popup_message'><center>Your session is about to expire.  Please click the button below to continue working without losing your session.</center></p>");
    $("#popup_window").append("<div class='buttons'><center><button id='continue' class='positive' type='submit'><img src='images/green-checkmark.png' alt=''/> Continue Working</button></center></div>");

    // attach action to button
    $("#continue").click(session_refresh);

    // display warning window
    popup_position(400, 300);
    $("#popup_window").css({ display: "block" }); //for safari using css instead of show
    $("#continue").focus();
    $("#continue").blur();

    // set pop-up timeout
    SESSION_ALIVE = false;
    window.setTimeout(popup_expired, WARNING_TIME);
}

Here is the code from the parent window:

<script language="javascript" type="text/javascript">
            var timer = false;
            window.reportChildActivity = function() {
                if (timer !== false) {
                    clearTimeout(timer);
                }
                //SESSION_ALIVE = true;
                timer = window.setTimeout(pop_init, SESSION_TIME);
            }
  </script>

Here is the code from the child window:

<script type="text/javascript">
    $(document).ready(function() {
        window.parent.reportChildActivity();
    });
</script>
<script type="text/javascript">
    $(document).bind("mousedown keydown blur", function() {        
            window.parent.reportChildActivity();
    });

The last script runs in a file (VB.NET ascx file) that builds the header/menu options for every page in our system.

The last line in the pop_init function clearly should be re-starting the timer, but for some reason it doesn't seem to be working.

Thanks for any help and insight you may have.

Forgot to add my code for the session_refresh function:

function session_refresh() {
    SESSION_ALIVE = true;
    $(".buttons").hide();
    $("#popup_message").html("<center><br />Thank you!  You may now resume using the system.<br /></center>");
    window.setTimeout(popup_remove, 1000);
    $("#popup_window").fadeOut("slow", function() { $('#popup_window,#popup_overlay').trigger("unload").unbind().remove(); });
    var timer = false;
    window.reportChildActivity = function() {
        if (timer !== false) {
            clearTimeout(timer);
        }
        timer = window.setTimeout(pop_init, SESSION_TIME);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

薆情海 2024-09-24 19:19:27

用户单击按钮后,您需要重新启动计时器。

You need to restart your timer after the user clicks the button.

南渊 2024-09-24 19:19:27

好吧,我似乎已经通过更改以下内容解决了问题:

var timer = false;
    window.reportChildActivity = function() {
        if (timer !== false) {
            clearTimeout(timer);
        }
        timer = window.setTimeout(pop_init, SESSION_TIME);
    }

改为:

if (timer !== false) {
            clearTimeout(timer);
        }
        timer = window.setTimeout(pop_init, SESSION_TIME);

我不需要像以前一样重新声明 reportChildActivity 函数。

Well, I seem to have fixed the problem by changing this:

var timer = false;
    window.reportChildActivity = function() {
        if (timer !== false) {
            clearTimeout(timer);
        }
        timer = window.setTimeout(pop_init, SESSION_TIME);
    }

to this:

if (timer !== false) {
            clearTimeout(timer);
        }
        timer = window.setTimeout(pop_init, SESSION_TIME);

I didn't need to re-declare the reportChildActivity function as I was.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文