4

Is it possible to ser a function to start in a given date and hour? How?

I thought about setTimeout, but what's the maximum time I can set?

--update

By the way, it's for a desktop application.

The Student
  • 27,520
  • 68
  • 161
  • 264

5 Answers5

4

I agree with JCOC611 - if you can make sure that your application does not close, then just get a Date object of when your alarm should go off and do something like this:

window.setTimeout(function() { soundAlarm() }, 
           alarmDate.getTime() - new Date().getTime());

I see no reason for this not to work, but a lot of people exalt a timer based solution where you have a short lived timer that ticks until the set time. It has the advantage that the timer function can also update a clock or a countdown. I like to write this pattern like this:

(function(targetDate) {
    if (targetDate.getTime() <= new Date().getTime()) {
        soundAlarm();
        return;
    }

    // maybe update a time display here?
    window.setTimeout(arguments.callee,1000,targetDate); // tick every second
})(alarmDate);

This is basically a function that when called with a target date to sound an alarm on, re-calls itself every second to check if the time has not elapsed yet.

Guss
  • 30,470
  • 17
  • 104
  • 128
  • I found this page, which my actual answer: http://www.webdeveloper.com/forum/showthread.php?t=173924 – The Student Jan 07 '11 at 11:39
  • Its a similar notion, though I think the guy's original problem was that he refreshes the page every 15 minutes or so even if you don't do anything else and thus he gets his timer unset when the page unloads. If you make sure your page never unloads (which makes sense for a desktop application) then you don't have that problem. But still - see my edit of the answer for a solution similar to the answers on the page you found. – Guss Jan 07 '11 at 15:17
  • 1
    re-calls itself every second: this will increase the stack without bound, which is a dangerous memory leak. Why not use `setInterval()` instead? – Roland Feb 03 '16 at 23:17
  • 1
    "Recalls" is a description of the effect, not the mechanic - please see the code. It sets a timer to rerun itself. The advantage of this approach over `setInterval` is that there is no interval registration code to keep track of and remember to unregister - just eventually the code will stop setting a timeout. Reduces global variable noise. – Guss Feb 04 '16 at 06:10
1
setTimeout(functionToCall,delayToWait)

As stated in Why does setTimeout() "break" for large millisecond delay values?, it uses a 32 bit int to store the delay so the max value allowed would be 2147483647

Community
  • 1
  • 1
Alberto Zaccagni
  • 30,779
  • 11
  • 72
  • 106
1

You should not relay on setTimeout for the actual alarm trigger but for a periodic function tracking the alarm. Use setTimeout to check the stored time for your alarm say every minute. Store that time in DB, file or server.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Miquel
  • 4,741
  • 3
  • 20
  • 19
1

Does setTimeout() have a maximum?

http://www.highdots.com/forums/javascript/settimeout-ecma-166425.html

It may surprise you that setTimeout is not covered by an ECMA standard, nor by a W3C standard. There are some holes in the web standards. This is one of them. I'm looking to the WHAT Working Group to fix this. See http://www.whatwg.org/specs/web-apps/current-work/

There doesn't seem to be a problem in setting the timeout value to something that is vastly greater than the MTBF of the browser. All that means is that the timeout may never fire.

http://javascript.crockford.com/ -Douglas Crockford

As others have mentioned, this isn't the way to handle the situation. Use setTimeout to check a date object and then fire the event at the appropriate time. Some code to play with is linked below.

http://www.w3schools.com/js/tryit.asp?filename=tryjs_timing_clock

Thomas Langston
  • 3,743
  • 1
  • 25
  • 41
0

Is there any server component to this at all? You could use setInterval to call something serverside on a regular basis via ajax, then pull back a date object and once it's finally in the past you could trigger your "alarm"

dstarh
  • 4,976
  • 5
  • 36
  • 68