3

We have a use case of running the java script thread once a month. But what we observe is if we provide 3.5 weeks of delay for the setinterval function it does not respect it and starts getting scheduling once a second. is it a bug? does it have any maxiumum limit for giving the delay?

Interestingly same works fine for setTimeOut. -

<!DOCTYPE html>
<html>
<body>

<input type="text" id="clock" />
<script>
    var int=setInterval(function(){clock()},1000*60*60*24*7*3.5);
    function clock()
    {
        var d=new Date();
        var t=d.toLocaleTimeString();
        document.getElementById("clock").value=t;
    }
</script>

<button onclick="int=window.clearInterval(int)">Stop</button>

</body>
</html>
3
  • 1
    You can just say clock instead of function(){clock()}. Commented Jun 21, 2013 at 10:22
  • In javascript with setInterval may occur some bugs, so will be better if you use setTimeout Commented Jun 21, 2013 at 10:25
  • possible duplicate of What is the maximum delay for setInterval? Commented Jun 21, 2013 at 11:07

1 Answer 1

1
1000*60*60*24*7*3.5 = 0x7e2bce00

Which is awfully close to 0x80000000, I wonder if 32 but signed arithmetic somewhere might be responsible.

I just checked the specification and the timeout for setInterval and setTimeout are both defined as long values which means signed 32 bit. It is unclear how a negativetimeout value would be treated.

From the WhatWG spec :

long setTimeout(Function handler, optional long timeout, any... arguments);

From the WebIDL standard :

The long type is a signed integer type that has values in the range [−2147483648, 2147483647].

To fix your problem I can make two suggestions:

Since setTimeout works, use it and retrigger at the end of the processing function

Alternatively setInterval of half the period you want and use a toggle to execute your code every other time.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.