javascript 带有jquery对话框的javascript中的会话超时警告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31609397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 14:03:31  来源:igfitidea点击:

session time out warning in javascript with jquery dialog

javascriptjqueryjquery-uitimeoutsession-timeout

提问by george

I am trying to implement a javascript session time out popup. Please help me. I am able to show popup for first time but when i click ok next time popup is coming in next one minute.For testing i gave 3 min time. Please help me resolve this.I am not able to reset the timer on mouseclick.

我正在尝试实现一个 javascript 会话超时弹出窗口。请帮我。我可以第一次显示弹出窗口,但是当我单击确定下一次弹出窗口将在下一分钟出现时。为了测试,我给了 3 分钟的时间。请帮我解决这个问题。我无法在鼠标点击时重置计时器。

</head>
<body>
<div id="dialog" style="display:none;" title="Dialog Title">Your session is going to expire in 10min</div>
<script>
var lefttime=4;
var interval;
setTimeout( 'ShowTimeoutWarning();', 180000 );


function ShowTimeoutWarning()
{

$( "#dialog" ).dialog( "open" );
return false;

}

$("#dialog").dialog({
autoOpen: false,
dialogClass: "no-close",
position: 'center' ,
title: 'session',
draggable: false,
width : 300,
height : 200,
resizable : false,
modal : true,
buttons: [
    {
      text: "OK",
      click: function() {
        ShowTimeoutWarning();
        $( this ).dialog( "close" );
     
      }
    }
  ]
});

document.onkeyup=setTimeout( 'ShowTimeoutWarning();', 180000 );
document.onkeydown=setTimeout( 'ShowTimeoutWarning();', 180000 );
document.click=setTimeout

</script>

回答by Tom Dyer

Do you mean something like this?

你的意思是这样的吗?

You need to set a variable to the setTimeout return value so that you can clear that timeout before setting another one.

您需要为 setTimeout 返回值设置一个变量,以便您可以在设置另一个超时之前清除该超时。

Javascript

Javascript

var timeoutID;
resetTimeout();

function resetTimeout(){
    if( timeoutID ) clearTimeout( timeoutID );
    timeoutID = setTimeout( ShowTimeoutWarning, 180000 );
}


function ShowTimeoutWarning() {
    $( "#dialog" ).dialog( "open" );
    return false;
}


$("#dialog").dialog({
    autoOpen: false,
    dialogClass: "no-close",
    position: 'center' ,
    title: 'session',
    draggable: false,
    width : 300,
    height : 200,
    resizable : false,
    modal : true,
    buttons: [{
        text: "OK",
        click: function() {
            ShowTimeoutWarning();
            $( this ).dialog( "close" ); 
        }
    }]
});

document.onkeyup   = resetTimeout;
document.onkeydown = resetTimeout;
document.onclick   = resetTimeout;

回答by Hary

Please find below code. time is set to 1 min. Before 55 sec popup message will come

请找到下面的代码。时间设置为1分钟。在 55 秒弹出消息之前

is postabck:

是邮局:

    if (!this.IsPostBack)
    {
            Session["Reset"] = true;
            Configuration config =WebConfigurationManager.    OpenWebConfiguration("~/Web.Config");
            SessionStateSection section =(SessionStateSection) config.GetSection("system.web/sessionState");

            int timeout = (int)section.Timeout.TotalMinutes * 1000 * 60;
            Page.ClientScript.RegisterStartupScript(this.GetType(), "onLoad", "DisplaySessionTimeout(" + timeout + ")", true);             
        }

Jquery & popup Message:

Jquery & 弹出消息:

 <div id="ExpireConfirm_Submit">
<table>
    <tr>
        <td style="width: 230px;">
            Your Session will expire in <span id="seconds"></span>&nbsp;seconds.<br />Do you
            want to logout?
        </td>
    </tr>
</table>

<script type="text/javascript">
 var sessionTimeout = "<%= Session.Timeout %>";
 function DisplaySessionTimeout(timeout) {
    var seconds = timeout / 1000;
    document.getElementsByName("seconds").innerHTML = seconds;
    setInterval(function () {
        seconds--;
        document.getElementById("seconds").innerHTML = seconds;
    }, 1000);
    setTimeout(function () {
        //Show Popup before 20 seconds of timeout.
        $("#ExpireConfirm_Submit").dialog({
            height: 200,
            width: 400,
            resizable: false,                
            modal: true,                
            title: "Session Expire Confirmation",                              
            open: function () {
                $('p#id1').html(sessionTimeout);
            },
            buttons: {
                "Yes": function () {                        
                    $(location).attr("href", "/Account/Logout").submit();
                    $(this).dialog("close");
                },
                "No": function () {                        
                    ResetSession();
                    $(this).dialog("close");
                }
            }
        }).prev(".ui-dialog-titlebar").css("background", "red");
    }, timeout - 55 * 1000);
    setTimeout(function () {
        $(location).attr("href", "/Account/Logout").submit();
    }, timeout);
};
function ResetSession() {
    window.location = window.location.href;
}    
</script>

This is best Example for show popup warning before session timeout

这是会话超时前显示弹出警告的最佳示例

回答by Jigarb1992

You should try this

你应该试试这个

<script>
$(document).ready(function () {

                var time = 30 * 1000 * 60; //session timeout 30 min
                var timeout;
                var isLogout = false;

                timeout = setTimeout(function() {
                    //Things you need to do
                        isLogout = true;

                }, time);

                $(document).on('click', function () {
                    if (!isLogout) {
                        clearTimeout(timeout);
                        timeout = setTimeout(function() {
                            //Things you need to do
                             isLogout = true;
                        }, time);
                    }
                });
            });
</script>