patternjavascriptMinor
Window timeout alert
Viewed 0 times
timeoutwindowalert
Problem
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
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);
}
})();Solution
A few points to make about this:
-
You can make
-
You call
If you're going to use globals (which are recommended against), declare them first, at the top.
All resulting in:
(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/delayedAlertcan be improved by movingslowAlertintodelayedAlertas 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
clearTimeoutcall 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 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();
})();Code Snippets
(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);
}
})();function delayedAlert() {
clearAlert();
timeoutID = window.setTimeout(function(){
alert("You may have been logged out due to inactivity. Click OK to refresh the page.");
}, 10000);
}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);
}delayedAlert();
var timeoutID;(function(){
var timeoutID;
// functions and whatnot
delayedAlert();Context
StackExchange Code Review Q#101789, answer score: 5
Revisions (0)
No revisions yet.