What happens to setTimeout when the computer goes to sleep?

后端 未结 7 1794
名媛妹妹
名媛妹妹 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:39

    Behaviour of JavaScript timers (setTimeout) in several scenarios.

    1. When the thread is free and the timeout fires: The timer is fired immediately after the timeout. It might have certain imprecision of about 0-5 ms (typical scenario).
    2. When the thread is super busy (huge loop) long enough to pass the timer timeout: The timer will be executed immediately after the thread is freed.
    3. When there is an alert: Same behaviour as 2.
    4. When the thread is paused because our laptop went to sleep: I have seen several things. But most common is total inaccuracy and ignore of the time spent during sleeping.

    Since timers in JavaScript are based on CPU ticks, and the CPU is sleeping, then the timer is completely paused and resumed as 'since nothing would have happen'.

提交回复
热议问题