4
\$\begingroup\$

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.

jsFiddle

(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);
 }
})();
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Aug 24, 2015 at 11:22
\$\endgroup\$
7
  • \$\begingroup\$ is this for a server side session timeout? \$\endgroup\$ Commented Aug 24, 2015 at 20:14
  • \$\begingroup\$ @Malachi yes, we have TMG server for that \$\endgroup\$ Commented 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\$ Commented 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\$ Commented 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\$ Commented Aug 25, 2015 at 13:21

2 Answers 2

5
\$\begingroup\$

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 moving slowAlert into delayedAlert 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 from delayedAlert; You could just put it in delayedAlert:
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 changing function delayedAlert() into var delayedAlert = function()

  • You call delayedAlert before timeoutID 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();
})();
answered Aug 24, 2015 at 11:47
\$\endgroup\$
1
\$\begingroup\$

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

answered Aug 26, 2015 at 13:25
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.