What happens to setTimeout when the computer goes to sleep?

后端 未结 7 1785
名媛妹妹
名媛妹妹 2020-11-29 01:04

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

7条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-29 01:38

    The behavior is based on both the browser and the operating system. The OS handle sleep and individual apps often don't account for it.

    What will most likely happen is that the OS will come back up with the same time remaining on the timer as when it was shut down. The other possibility is that it won't fire at all.

    If it is really a concern, you will probably want to be better safe than sorry and store a time stamp of when the token was initialized and use setInterval to check it periodically (say twice a minute).

    However, security should not be just a client side thing. Make sure that your server throws an error if an old / invalid token is used and that the Ajax behaves appropriately in response.

    [edit] I agree with the other post that it might fire immediately on the next tick. Resig's blog post is very good.

提交回复
热议问题