-1

I have this functionality built in Jquery/ javascript where a user is notified when their session is about to be timed out. Then they are given an option to extend it. If they don't make a choice the pop up closes itself and the browser is redirected to the login page. It worked perfectly fine for a while. But now I noticed it works correctly only if I am active on the computer. If the computer is left unattended for an entire day, the pop up does not begin the countdown until the user unlocks the computer and logs in again.

Is anybody aware of this behavior where ie stops executing javascript when the computer is left unattended for a long time?

Update: Is there a way to keep the tab from sleeping? Without that, the browser won't be able to redirect at the right time.

developer747
  • 15,419
  • 26
  • 93
  • 147

2 Answers2

1

setTimeout only works when the tab is active. In some browsers even changing tab will make it stop counting. So not only if you are on the computer but if you're not on the specific page it might not work. Also on mobile devices with multitasking it's bound to fail, forget about tabs, applications often go to suspended mode. Take a look at this question, it offers the same solution as Luka with a code example:

How can I make setInterval also work when a tab is inactive in Chrome?

You might want to do two checks on the time passed, one to check if you need to show the popup, and one to close the popup, using the total time passed instead of having a different count down.

Community
  • 1
  • 1
Geekfish
  • 2,173
  • 23
  • 28
  • Even if I check the time passed, the javascript should be able to execute at the right time to be able to redirect user to the login page. With the javascript in suspended mode, checking time will not solve my problem :( – developer747 Jun 19 '12 at 13:48
  • You cannot redirect if the page is not active, you shouldn't expect the browser to do that. If your interval is something like every two seconds (if it's a small check it won't be expensive for performance) then the redirect will happen as soon as the user goes back to the tab. – Geekfish Jun 19 '12 at 15:45
  • what does "the right time" means anyway? If your concern is security then you should ask yourself if your server is also timing out the session independently of any javascript calls. If it is then I cannot see any security implications, if not then your problem lies elsewhere. – Geekfish Jun 19 '12 at 15:48
  • In the specific scenario I am working in, it is ideal to have the redirect at the expected time. It is not a security threat. – developer747 Jun 19 '12 at 17:40
0

i would suggest logging the current time when the page is loaded and then calling a function every 10 seconds of so that checks if the time passed is more than x amount, the reason your problem occurs is most likely because the default timeout function only counts down while the page is being rendered.

Luka
  • 341
  • 2
  • 11
  • I don't understand why you got a down vote on this one. It might be tweaked further but the general idea of using `setInterval` instead to perform comparison of datetimes is much more reliable and answers this question. Edit: typo – Geekfish Jun 19 '12 at 13:22
  • 1
    I don't really care about my reputation, i just want to help :p – Luka Jun 19 '12 at 13:26