Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens to setTimeout when the computer goes to sleep?

In a modern web browser, suppose I do a setTimeout for 10 minutes (at 12:00), and 5 minutes later put the computer to sleep, what should happen when the system wakes up again? What happens if it wakes up before the 10 minutes are up (at 12:09) or much later (at 16:00)?

The reason I'm asking is because I'd like to have a new authentication token requested every 10 minutes, and I'm not sure if the browser will do the right thing and immediately request a new token if it wakes up after a long time.

Clarifications: I don't wan't to use cookies - I'm trying to build a web service here; and yes, the server will reject old and invalid tokens.

like image 322
Sudhir Jonathan Avatar asked Jun 14 '11 16:06

Sudhir Jonathan


People also ask

What happens when setTimeout is 0?

Calling setTimeout with a delay of 0 (zero) milliseconds doesn't execute the callback function after the given interval. The execution depends on the number of waiting tasks in the queue.

Does setTimeout run forever?

The setTimeout() is executed only once. If you need repeated executions, use setInterval() instead.

Do we need to clear setTimeout?

You don't actually need to use clearTimeout , you only use it if you wish to cancel the timeout you already set before it happens. It's usually more practical to use clearInterval with setInterval because setInterval usually runs indefinitely.

How does the setTimeout work?

The setTimeout() sets a timer and executes a callback function after the timer expires. In this syntax: cb is a callback function to be executed after the timer expires. delay is the time in milliseconds that the timer should wait before executing the callback function.


2 Answers

As far as I've tested, it just stops and resumes after the computer wakes up. When the computer awakes the setInterval/setTimeout is unaware that any time passed.

I don't think you should rely on the accuracy of setTimeout/Interval for time critical stuff. For google chrome I discovered recently that any timeout/interval (that is shorter than 1s) will be slowed down to once a second if the tab where it's activated looses focus.

Apart from that the accuracy of timeouts/intervals is dependent on other functions running etc. In short: it's not very accurate.

So using interval and timeouts, checking the time against a starttime within the function started by it would give you better accuracy. Now if you start at 12:00, the computer goes to sleep and wakes up at 16:13 or so, checking 16:13 against 12:00 you are certain you have to renew the token. An example of using time comparison can be found here

like image 169
KooiInc Avatar answered Sep 28 '22 06:09

KooiInc


Compare current datetime against datetime when the page was loaded, like so:

//Force refresh after x minutes. var initialTime = new Date(); var checkSessionTimeout = function () {     var minutes = Math.abs((initialTime - new Date()) / 1000 / 60);     if (minutes > 20) {         setInterval(function () { location.href = 'Audit.aspx' }, 5000)     }  }; setInterval(checkSessionTimeout, 1000); 
like image 30
Ben Avatar answered Sep 28 '22 05:09

Ben