Warn user before session in spring mvc
I have a web application implemented in Spring MVC, the JSP with a default timeout of 30 minutes.
I need to show an alert in the UI saying "Your session will end in 5 minutes. Click OK to continue" if the session expires in 5 minutes.
What's the best way to achieve this?
I found some answers here
Just wondering if there is another better way to do this.
+3
source to share
1 answer
try this below code works well for me
var cookieName = 'sessionMsg';
var message = 'Your session is Expires 5 min, click OK to continue';
function getCookie(name)
{
var name = name + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++)
{
var c = ca[i].trim();
if (c.indexOf(name)==0) return c.substring(name.length,c.length);
}
return "";
}
function setSessionPrompt() {
var timeout = getCookie(cookieName) - new Date().getTime();
setTimeout(function(){
if (new Date().getTime() < getCookie(cookieName)) {
setSessionPrompt();
} else {
if(confirm(message)) {
// do your action here
}
}
}, timeout);
}
setSessionPrompt();
0
source to share