使用jquery对话框在javascript中进行会话超时警告

时间:2015-07-24 11:28:00

标签: javascript jquery jquery-ui timeout session-timeout

我正在尝试实现一个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>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

你的意思是这样吗?

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

<强>的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;

答案 1 :(得分:0)

请查看以下代码。时间设定为1分钟。在55秒之前弹出消息

是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&amp;弹出消息:

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

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

答案 2 :(得分:0)

你应该试试这个

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