I'm trying to add 'session ending warning' and 'session expired' pop-ups to a site that displays secure data. The session timeout is after an hour. The pop-ups are controlled by javascript using the code below, which checks the time left in the session every minute. BUT... the call to the function that checks the time left in the session is apparently undefined, and I can't understand why. I'm basing my code on an example I found online and another javascript/jquery based pop-up on the site I'm working on that was written by someone else. IE just tells me there's an object expected on the page, but with Firefox, I'm using Firebug and it's saying the function checkSessionTime() is not defined. With both browsers, my pop-ups are obviously not working. My javascript/jquery knowledge is limited, so any help anyone can offer would be greatly appreciated. Thanks.
$(document).ready(function() {
//total length of session (1 hour) in seconds
var sessionLength = 3600;
//time warning shown (120 = warning box shown 120 seconds before session ends)
var warning = 120;
//time redirect forced (5 = redirect forced 5 seconds after session ends)
var forceRedirect = 5;
//time session started
var pageRequestTime = new Date();
//session timeout length
var timeoutLength = sessionLength*1000;
//set time for first warning, ten seconds before session expires
var warningTime = timeoutLength - (warning*1000);
//force redirect to log in page length (session timeout plus 5 seconds)
var forceRedirectLength = timeoutLength + (forceRedirect*1000);
//set number of seconds to count down from for countdown ticker
var countdownTime = warning;
//warning dialog open; countdown underway
var warningStarted = false;
//event to check session time variable declaration
var checkSessionTimeEvent;
// jQuery UI Dialog
var dialogWarningOpts = {
autoOpen: false,
width: 400,
modal: true,
bgiframe: true,
draggable: true,
resizable: false,
open: function() {},
close: function() {},
buttons: {
"Restart Session": function() {
location.reload();
}
}
};
var dialogExpiredOpts = {
autoOpen: false,
width: 400,
modal: true,
bgiframe: true,
draggable: true,
resizable: false,
open: function() {},
close: function() {
location.reload();
//window.location="login.php?expired=true";
},
buttons: {
"Login": function() {
location.reload();
//window.location="login.php?expired=true";
}
}
};
function checkSessionTime() {
//get time now
var timeNow = new Date();
//event create countdown ticker variable declaration
var countdownTickerEvent;
//difference between time now and time session started variable declartion
var timeDifference = 0;
timeDifference = timeNow - pageRequestTime;
if (timeDifference > warningTime && warningStarted === false){
//call now for initial dialog box text (time left until session timeout)
countdownTicker();
//set as interval event to countdown seconds to session timeout
countdownTickerEvent = setInterval("countdownTicker()", 1000);
$('#dialogWarning').load('dialogWarning.php').dialog(dialogWarningOpts);
warningStarted = true;
} else if (timeDifference > timeoutLength) {
//close warning dialog box
if ($('#dialogWarning').dialog('isOpen')) $('#dialogWarning').dialog('close');
$('#dialogExpired').load('dialogExpired.php').dialog(dialogExpiredOpts);
//clear (stop) countdown ticker
clearInterval(countdownTickerEvent);
}
if (timeDifference > forceRedirectLength) {
//clear (stop) checksession event
clearInterval(checkSessionTimeEvent);
//force relocation
window.location="login.php?expired=true";
}
}
function countdownTicker()
{
//put countdown time left in dialog box
$("span#dialogText-warning").html(countdownTime);
//decrement countdownTime
countdownTime--;
}
//event to check session time left (times 1000 to convert seconds to milliseconds)
checkSessionTimeEvent = setInterval("checkSessionTime()",60*1000);
});