How can I improve this window timeout alert code? I need to add it to some third-party master page and don't want to add a separate file for it.
It must be JS only and IE-8 supported.
(function () {
// timedRefresh(10); //3600000
delayedAlert();
var timeoutID;
function delayedAlert() {
clearAlert();
timeoutID = window.setTimeout(slowAlert, 10000);
}
function slowAlert() {
alert("You may have been logged out due to inactivity. Click OK to refresh the page.");
}
function clearAlert() {
window.clearTimeout(timeoutID);
}
})();
-
\$\begingroup\$ is this for a server side session timeout? \$\endgroup\$Malachi– Malachi2015年08月24日 20:14:19 +00:00Commented Aug 24, 2015 at 20:14
-
\$\begingroup\$ @Malachi yes, we have TMG server for that \$\endgroup\$Mathematics– Mathematics2015年08月25日 10:02:45 +00:00Commented Aug 25, 2015 at 10:02
-
\$\begingroup\$ I don't think you are hosting a website on TMG, are you? when you say Master page I assume you are talking about ASP (Active Server Pages) that make use of Microsoft's .NET framework. why aren't you handling session timeout? \$\endgroup\$Malachi– Malachi2015年08月25日 13:16:36 +00:00Commented Aug 25, 2015 at 13:16
-
\$\begingroup\$ @Malachi following what manager says... we are using SharePoint master page, where i am going to add this code, its a quick fix, we may come back to it later on, but atm it serves the purpose :) \$\endgroup\$Mathematics– Mathematics2015年08月25日 13:18:09 +00:00Commented Aug 25, 2015 at 13:18
-
\$\begingroup\$ again, I ask, why aren't you using the session timeout. you are making a random timeout value that doesn't correlate to anything except for a magic number. \$\endgroup\$Malachi– Malachi2015年08月25日 13:21:27 +00:00Commented Aug 25, 2015 at 13:21
2 Answers 2
A few points to make about this:
(function () { // timedRefresh(10); //3600000 delayedAlert(); var timeoutID; function delayedAlert() { clearAlert(); timeoutID = window.setTimeout(slowAlert, 10000); } function slowAlert() { alert("You may have been logged out due to inactivity. Click OK to refresh the page."); } function clearAlert() { window.clearTimeout(timeoutID); } })();
slowAlert
/delayedAlert
can be improved by movingslowAlert
intodelayedAlert
as follows:
function delayedAlert() {
clearAlert();
timeoutID = window.setTimeout(function(){
alert("You may have been logged out due to inactivity. Click OK to refresh the page.");
}, 10000);
}
- I can't see why the
clearTimeout
call deserves its own function, when it only gets called fromdelayedAlert
; You could just put it indelayedAlert
:
function delayedAlert() {
window.clearTimeout(timeoutID);
timeoutID = window.setTimeout(function(){
alert("You may have been logged out due to inactivity. Click OK to refresh the page.");
}, 10000);
}
You can make
delayedAlert
an anonymous function, so that it cannot be called other than inside that block by changingfunction delayedAlert()
intovar delayedAlert = function()
You call
delayedAlert
beforetimeoutID
is defined:
delayedAlert(); var timeoutID;
If you're going to use globals (which are recommended against), declare them first, at the top.
(function(){
var timeoutID;
// functions and whatnot
delayedAlert();
All resulting in:
(function(){
var timeoutID;
var delayedAlert = function() {
window.clearTimeout(timeoutID);
timeoutID = window.setTimeout(function(){
alert("You may have been logged out due to inactivity. Click OK to refresh the page.");
}, 10000);
}
delayedAlert();
})();
I know that you are writing this for an ASPX page, so I am suggesting that you use the Server's session timeout instead of using a magic number for the timeout.
in this function:
function delayedAlert() { clearAlert(); timeoutID = window.setTimeout(slowAlert, 10000); }
you could write it like this
function delayedAlert() {
var sessionTimeout = "<%= Session.Timeout %>";
clearAlert();
timeoutID = window.setTimeout(slowAlert, sessionTimeout);
}
this way you won't timeout too early or timeout too late, you will always timeout almost exactly as the session times out.
Extra Information was gathered from a discussion posted in a chat room along with the comments to the OP