计算机进入睡眠状态时setTimeout会发生什么? [英] What happens to setTimeout when the computer goes to sleep?

查看:321
本文介绍了计算机进入睡眠状态时setTimeout会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在现代网络浏览器中,假设我执行 setTimeout 10分钟(在12:00),5分钟后让计算机进入睡眠状态,会发生什么什么时候系统再次唤醒?如果它在10分钟之前(12:09)或更晚的时间(16:00)醒来会发生什么?

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)?

我问的原因是因为我想每10分钟要一次新的身份验证令牌,我不确定浏览器是否会做正确的事情,并且如果它在很长一段时间后醒来,立即请求新的令牌。

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.

澄清:我不想使用cookies - 我正在尝试在这里建立一个Web服务;是的,服务器将拒绝旧的和无效的令牌。

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.

推荐答案

据我测试,它只是停止并恢复电脑醒来后。我想这意味着会话取决于 setTimeout / Interval 计数器从计算机睡着时开始计时。

As far as I've tested, it just stops and resumes after the computer wakes up. I suppose that means for a session depending on setTimeout/Interval the counter ticks on from the time the computer fell asleep.

我认为您不应该依赖 setTimeout / Interval 的准确性来获取时间关键的东西。对于谷歌浏览器,我最近发现任何超时/间隔(即如果激活的标签失去焦点,则短于1秒的速度将减慢到每秒一次。

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.

因此,使用间隔和超时,检查由它启动的函数内的开始时间的时间将提供更好的准确性。现在,如果你从12:00开始,计算机将进入睡眠状态并在16:13左右醒来,检查16:13对抗12:00您确定必须更新令牌。可以在这里找到使用时间比较的示例

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

这篇关于计算机进入睡眠状态时setTimeout会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆