upper bound on setTimeout() length?

M

masonsimon

I'm trying to use the following function to update an Opera widget that
I'm writing, but it doesn't seem to work.


function runAt(time_to_run_at, what_to_run)
{
//time_to_run_at is a Date
//what_to_run is either a function reference or a string of JavaScript

var now = new Date();

time_until_run = time_to_run_at.getTime() - now.getTime();

window.setTimeout(what_to_run, time_until_run);
}


The timeouts that I'm setting with this are many hours long. I've
tested the function with shorter time lengths and it seems to work
fine. I've noticed this behavior in Firefox 1.5 and Opera 9 beta 2.
From this I conclude that setTimeout just dies when its timeouts are
too long.

Does anyone else have experience with this? Is there some maximum
timeout length that I shouldn't exceed?

I'm thinking that I could rewrite the function to set a bunch of
subsequent timeouts that would sum up to the total timeout that I want,
but I'm lazy, so if anyone already knows a good maximum timeout to set,
I'd appreciate it if you shared that bit of knowledge.

Any help is appreciated. Thanks!

-Mason
 
V

VK

I'm trying to use the following function to update an Opera widget that
I'm writing, but it doesn't seem to work.


function runAt(time_to_run_at, what_to_run)
{
//time_to_run_at is a Date
//what_to_run is either a function reference or a string of JavaScript

var now = new Date();

time_until_run = time_to_run_at.getTime() - now.getTime();

window.setTimeout(what_to_run, time_until_run);
}


The timeouts that I'm setting with this are many hours long. I've
tested the function with shorter time lengths and it seems to work
fine. I've noticed this behavior in Firefox 1.5 and Opera 9 beta 2.

Please make sure that you are using the latest version of Firefox
(1.5.0.2 now) Untill that version there was a bug for extra long
timers, see <https://bugzilla.mozilla.org/show_bug.cgi?id=318419>

In this discussion led to the bug fix there is a lot of information
about timers on different OS.

Taking the smallest value in the worst case the longest timer you can
have in JavaScript/JScript is approx. 11 hours - unless I read the
linked thread wrongly.
 
S

Stephen Chalmers

The timeouts that I'm setting with this are many hours long.

Presumably this is for an environment in which it is certain that a
page will remain loaded for many hours.
I'm thinking that I could rewrite the function to set a bunch of
subsequent timeouts that would sum up to the total timeout that I want,

can't you use setInterval to repeat a statement that compares the
target time with the current time every few seconds?
 
M

masonsimon

Thanks for the replies! I feel like an idiot, but I just checked my
code and it looks like my logic was the problem, and not setTimeout.
Whoops
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top