I'm writing some JavaScript that manages scheduled events. The page should update when the event is due.
I'm currently using window.setTimeout(cb, scheduledTime - now)
. This works fine normally, but if the laptop is suspended then the delay is too long.
Example:
- I schedule an event for tomorrow, at 9am.
- I put the laptop to sleep.
- The next day at 9am, I resume the laptop. The event should now be due, but my code is still sleeping.
- Several hours late, I get notified of the event.
I could just poll every 60s or so, but is there some way to:
- Set a timeout that isn't delayed by suspend, or
- Get notified when the system is resumed?
(test script showing the problem: https://stackoverflow.com/a/29656299/50926)