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 ing 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.
</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>
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 ing 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.
</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>
Share
Improve this question
edited Jul 24, 2015 at 11:33
george
asked Jul 24, 2015 at 11:28
georgegeorge
111 gold badge1 silver badge5 bronze badges
1
- 1 Can you show us some code, html, and javascript, please? – Dean.DePue Commented Jul 24, 2015 at 11:30
3 Answers
Reset to default 1Do 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.
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;
Please find below code. time is set to 1 min. Before 55 sec popup message will e
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:
<div id="ExpireConfirm_Submit">
<table>
<tr>
<td style="width: 230px;">
Your Session will expire in <span id="seconds"></span> 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
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>