Javascript SetTimeout delay not working when trying to implement session auto logout

Javascript SetTimeout delay not working when trying to implement session auto logout

  

Hi Team,

I'm trying to implement automatic logout for mobile app for an N minutes. I used javascript node to call setTimeout() javascript function. I've set the delay to 1 minute but during runtime the message that is supposed to show in 1 minute, showed up instantly. It seems the javascript delay did not take effect.

I'm trying to logout a user but would like to ask first user to resume or not.

Thanks!


Hi Rian

I have used setTimeout before successfully. Could you post your JS code, where are you setting it up, and if you're navigating away from the screen so that we can try to figure out what is going on?


Regards,

   CLSJ

Hi Rian,

setTimeout() works fine (I've used it a couple of times), so I think you must've made an error somewhere. My code:

$parameters.o_IntervalVar = window.setInterval(function(){
  $actions.MyAction();
}, $parameters.a_RefreshRate * 1000);

However, the bigger problem here is that setTimeout() is bound to a Screen. So if the user navigates to a different Screen, and the timeout hits, there's no target and you'll get a (silent) error (which will be visible in the logging). So basically you cannot use setTimeout() for this.

That's why the code above has an output o_IntervalVar, as it is needed for removing the interval when the screen is destroyed:

clearInterval($parameters.a_IntervalVar);

I'm actually executing the settimeout() upon clicking login button on login screen. So would that mean since a succesful login navigates to another screen, it cancels out the settimeout settin on login screen?

What I did is I got 3 global client actions, 1.) startSession(), 2.) confirmSession() - asks users if he would like to resume session if yes, calls startSession() again if not it calls the 3.) endSession(). 

sample code:


function startSession(){
    var a = setTimeout(checkSession(),60000);
}
---------------------------------------------------------------
function checkSession(){
    if(confirm("Resume Session?")){
        startSession();
    }
    else{
        endSession();
    }
   
}

---------------------------------------------------------------

function endSession(){

   /*calls logout client action */

}


really appreciate your help on this.

Yes, navigating to a different screen does indeed cancel out the timeout (if you go into the chrome console you'll see an out of scope exception). 

In order to have the timeout constantly running regardless of the screen you're at, set the timeout on the OnReady of your layout and be sure to clear the timeout on the OnDestroy of the same layout with the logic that Kilian suggested. You might also want to check the OnResume event to make sure that the timeout has kept running even when the app went to the background.


Hope this helps!

   CLSJ

Hi Carlos,

I'm using different layouts for some pages/screens. Should I have single layout for these pages in order to implement this?

Thanks!

You could include it in every layout you need the timer to be running. As long as the timer is cleared every time it is set and then navigated away, you should have no problem if it is another layout setting the timer on the next screen. 

Although it is possible to make this work, by "carrying over" the timeout from screen to screen via the aformentioned method, and intermediately storing the current value in the local database (and retrieving it again), I would strongly advise against it. It complicates the logic on your screen, you need to manually code it for every screen, etc.

Also, auto-logout is really, really annoying for a user, and unless you have an app that needs very high security, I wouldn't implement it.

As a last point, as long as the user is interacting with the screen, do you really want to log them out? Like, in the middle of entering a form, then bam, logged out because time's up? No user would want to use your app anymore! Auto-logout, if needed, should be triggered after the user is idle for a certain period of time, not unconditionally!

Kilian Hekhuis wrote:

Although it is possible to make this work, by "carrying over" the timeout from screen to screen via the aformentioned method, and intermediately storing the current value in the local database (and retrieving it again), I would strongly advise against it. It complicates the logic on your screen, you need to manually code it for every screen, etc.

Also, auto-logout is really, really annoying for a user, and unless you have an app that needs very high security, I wouldn't implement it.

As a last point, as long as the user is interacting with the screen, do you really want to log them out? Like, in the middle of entering a form, then bam, logged out because time's up? No user would want to use your app anymore! Auto-logout, if needed, should be triggered after the user is idle for a certain period of time, not unconditionally!


Since the timeout is checking for user inactivity, it is indeed not needed to carry over the value that the timer was at the last time the user navigated away from the screen. It wouldn't matter that the timer is reset, therefore the only thing needed is including the appropriate actions on the onReady and OnDestroy without any DB querying.


@Rian have you tried changing the session timeout value to match the needs off your session timeout? It would of course not ask the user if he wanted to continue, but would indeed simplify the implementation.


Regards,

   CLSJ