Reputation: 444
I have a php header and a js function that logs people out if they don't move their mouse for 10 minutes, I want it to alert them when there is 1 minute left but can't get it working. Below is the code that works.
The onload is in the header.
<body onload="set_interval()"
onmousemove="reset_interval()"
onclick="reset_interval()">
Then the js
var timer = 0;
function set_interval() {
timer = setInterval("auto_logout()" { alert('You are being logged out, check if you have an inactive page') }, 60000);
// milliseconds
}
function reset_interval() {
if (timer != 0) {
clearInterval(timer);
timer = 0;
timer = setInterval("auto_logout()" { alert('You are being logged out, check if you have an inactive page') }, 60000);
}
}
function auto_logout() {
window.location = "logout.php";
}
It all works nicely if I leave out the
{ alert('You are being logged out, check if you have an inactive page') }
so I know the problem is there, but unsure how to fix it, please assist. Also even if it did work it's alerting them at the end of the interval (I think), while I want them to get a prior alert so they can move their mouse.
Upvotes: 0
Views: 95
Reputation:
My suggestion is to use setTimeout()
instead:
var track;
var msg = function() {
alert("You will be logged out in one minute !");
};
var logout = function() {
alert("You have been logged out!");
location.replace('logout.php');
}
var trace = function() {
console.log('trace ...');
track = setTimeout(logout, 1000 * 60);
};
var retrace = function() {
console.log('retrace ...');
clearTimeout(track);
trace();
};
html,
body {
width: 100%;
height: 100%;
background: #ccc;
border: 1px solid #aaa;
}
<body onload="msg();trace()" onmousemove="retrace()" onclick="retrace();">Move your mouse here ...</body>
Upvotes: 2
Reputation: 413
var timer;
function set_interval() {
timer = setInterval(auto_logout, 60000);
}
function reset_interval() {
clearInterval(timer);
set_interval();
}
function auto_logout() {
alert('You are being logged out, check if you have an inactive page');
window.location = "logout.php";
}
Upvotes: 3