0

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:

  1. I schedule an event for tomorrow, at 9am.
  2. I put the laptop to sleep.
  3. The next day at 9am, I resume the laptop. The event should now be due, but my code is still sleeping.
  4. 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)

Community
  • 1
  • 1
Thomas Leonard
  • 7,068
  • 2
  • 36
  • 40

1 Answers1

0

You could check the current time every second or so, and if it is the desired amount (e.g. 9:00 AM), alert:

function alert_time(month, day, year, hour, minute){
    currentDate = new Date();
    if(currentDate.getMonth() == month && currentDate.getDate() == day && currentDate.getYear() == year && currentDate.getHour() == hour && currentDate.getMinute() == minute){
        alert("Time up!")
    else{
        setTimeout(function (){alert_time(month, day, year, hour, minute);}, 10);
}
A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76