What happens to setTimeout when the computer goes to sleep?

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

    Based on Ben's answer, I created the following util. You can tweak the sampling duration, however I use it just like this for token refreshing:

    const absoluteSetInterval = (handler, timeout) => {
      let baseTime = Date.now();
      const callHandler = () => {
        if (Date.now() - baseTime > timeout) {
          baseTime = Date.now();
          handler();
        }
      };
      return window.setInterval(callHandler, 1000);
    };
    
    const absoluteClearInterval = (handle) => window.clearInterval(handle);
    

提交回复
热议问题