Javascript for session timeout

Re: Javascript for session timeout

by Ronald Vyhmeister -
Number of replies: 2
OK... So more (and hopefully final) data... The problem occurs ONLY when a machine goes to sleep... if the machine is hibernated, the SCORM player will detect the dropped network connection and terminate the playing upon restart...

So we've written a script that we've inserted in the additional HTML headers... This script will detect when there is a time gap (when can only be caused by sleeping)... and then prompt the user to close the SCORM window and restart it.

We realize that this approach may nail a user that put their machine to sleep to move from one room to another... but they're two clicks away from being back to where they left off... and it totally avoids the problem of the user who left the SCORM package running and returned many hours later... and then screams because their progress is lost...

So here's the script for those who may want it. Will only run in the SCORM player...

Hope this helps somebody in the future!
In reply to Ronald Vyhmeister

Re: Javascript for session timeout

by Richard Pochepan -

Hi Ronald,

How may I view the script which you referred to in your previous post?

Thanks!

In reply to Richard Pochepan

Re: Javascript for session timeout

by Ronald Vyhmeister -
Sorry... had not realized it hadn't pasted properly... here goes... This does not look at the timeout... it essentially says "you put your machine to sleep, so we don't know about timeouts, so restart the SCORM package".

Note that apparently Safari on mobile devices (iPads and iPhones) puts javascript to sleep whenever you go do something else... so you have this alert...


$(document).ready(function () {
//get current page's url
var url = window.location.href;
// detect if we are on the SCORM player page
if (url.includes('player.php')) {
// initialize the saved time
var saveDte = Date.now()
function checkTime() {
//get current time
var currentDte = Date.now();
// if the time elapsed is more than 11 seconds, then there was a time issue
var elapsedTime = ((currentDte - saveDte) / 1000);
if(elapsedTime > 11) {
console.log(elapsedTime + ' seconds' );
alert('Your computer appears to have gone to sleep. Please click ok to close this window and return to Moodle. You can resume your training by selecting the course link in Moodle.');
window.close();
} else {
console.log('date ok');
console.log(elapsedTime + ' seconds' );
//set new save date
saveDte = currentDte;
}
}
var timeoutId = setInterval(checkTime, 10000);
}
});