function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Demo29Demo29 

Redirect user after inactivity

Hi All

 

I am currently trying to create a sites page that redirects the user after a certain amount of inactivity, and I was wondering if anyone found a good way of doing this?

 

I tried using a class that would be activated when the user clicks on the form then waits until it sends back the pageReference, but the problem is it wouldn't reset when the user clicked the form again.

 

I then turned to javascript, creating a simple function that resets a timer, then waits before redirecting. The problem is the form contains outputPanels that render dynamically (all contained in the 1 form tag), and while the javascript works fine for the first visible panel, when the second is rendered, it stops reseting the first timer when the form is clicked on.

 

Small example of the code used:

 

 

<apex:page>
<script>
var timerId =0;
function tojs() {
clearTimeout(timerId);
timerId = setTimeout('redirect()',10000);
//set to 10 seconds for testing
}

function redirect() {
location.href = '../apex/FormTimeOut';
}
</script>
<apex:form id="frm" onclick="tojs()">
<apex:outputPanel rendered="{!render1}">
<apex:inputCheckbox value="{!render2}">
<apex:actionSupport event="onchange" action="{!render2ndPanel}" rerender="frm"/>
</apex:inputCheckbox>
</apex:outputPanel>
<apex:outputPanel rendered="{!render2}">
tra la la la
</apex:outputPanel>
</apex:form>




</apex:page>

 

So with this example the function tojs() would work as normal when the first outputPanel is clicked on (i.e. resetting the timer), but when the checkbox is ticked and the 2nd panel is rendered the tojs() timer is no longer reset. Could this be due to rerender?

 

Has anyone come up with a good way of doing this?

 

Thanks!

 

 

 

 

Message Edited by Demo29 on 09-30-2009 10:07 AM
Message Edited by Demo29 on 09-30-2009 10:07 AM
David VPDavid VP
Why don't you attach a javascript onkeydown() and onmousedown() listener to the entire body and have that reset your timer ? Then you don't have to worry about the different panels. Whenever the user types or clicks in the page, it will reset your timer.
Demo29Demo29
Thanks for the reply, unfortunately I've tried attaching the timer reset to an outputPanel that encompasses the entire form, but it still seems to start a seperate timer instance after a rerender. I've found one way round it by adding some code in the controller that is activated by an actionFunction on the onclick event of the form:

public Datetime tTime {get;set;}
public PageReference toTest() {
Datetime cTime = Datetime.now();
if (cTime > tTime.addMinutes(20)) {
if (Edit == true) {
List<Database.DeleteResult> ddr = Database.Delete(editApps,false);
}
PageReference test = new PageReference('/apex/appFormTimeOut');
test.setRedirect(true);
return test;
} else {
tTime = cTime;
return null;
}

}

 where tTime is set as the current time on the page render. This works for the entire form, however the redirect only occurs when the user clicks on the form after 20 mins of activity, rather than instantly redirecting after the inactivity.

 

Does anyone know if there is a way I could implement a wait() functionality in apex?

 

Thanks