Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java script setInterval setting the delay?

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>
like image 745
user2508580 Avatar asked Aug 13 '26 15:08

user2508580


1 Answers

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.

like image 85
HBP Avatar answered Aug 15 '26 05:08

HBP



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!