1

I have setTimeout function which need to execute greeting function after a specific milliseconds 2958219351 but it execute function directly without waiting for 2958219351 but when i specific setTimeout function with 5000 milliseconds it wait for 5 seconds then execute function.

Below is code for 2958219351 milliseconds

<!DOCTYPE html>
<html>
<body>

<h2>The setTimeout() and clearTimeout() Methods for 2958219351 milliseconds</h2>

<p>Click "Stop" to prevent myGreeting() to execute.</p>

<h2 id="demo"></h2>

<button onclick="myStopFunction()">Stop!</button>

<script>
const myTimeout = setTimeout(myGreeting, 2958219351);

function myGreeting() {
  document.getElementById("demo").innerHTML = "Happy Birthday!"
}

function myStopFunction() {
  clearTimeout(myTimeout);
}
</script>

</body>
</html>

Below is code for 5000 milliseconds

<!DOCTYPE html>
<html>
<body>

<h1>The Window Object</h1>
<h2>The setTimeout() and clearTimeout() Methods</h2>

<p>Click "Stop" to prevent myGreeting() to execute. (You have 5 seconds)</p>

<h2 id="demo"></h2>

<button onclick="myStopFunction()">Stop!</button>


<script>
const myTimeout = setTimeout(myGreeting, 5000);

function myGreeting() {
  document.getElementById("demo").innerHTML = "Happy Birthday!"
}

function myStopFunction() {
  clearTimeout(myTimeout);
}
</script>

</body>
</html>

I don't know why it execute directly setTimeout function without waiting for 2958219351 milliseconds

Ahsan Ullah
  • 158
  • 4
  • 15
  • 3
    `2958219351 > 2147483647` and `2147483647` is the maximum delay. – VLAZ Oct 24 '22 at 07:06
  • https://developer.mozilla.org/en-US/docs/Web/API/setTimeout#maximum_delay_value – Martheen Oct 24 '22 at 07:07
  • @CherryDT Argh... Missed that part. I blame it on the headache^^ – Andreas Oct 24 '22 at 07:10
  • I want to wait for 35 days to execute greeting function but as @VLAZ mentioned max delay is 2147483647. So i want know what could be the alternative solution to setTimeout to wait for 35 days? – Ahsan Ullah Oct 24 '22 at 07:11
  • You expect a user to keep your site open continuously without closing it or restarting their computer for over a month…!? – deceze Oct 24 '22 at 07:12
  • 1
    @AhsanUllah simple check once a day/hour/minute/second/whatever interval and see if enough time has elapsed. `if (startTime - nowTime >= expectedDelay) { /* do stuff */ }`. Also, since you can save the start time, it helps with people who close tabs at least once a month. I've heard there is at least a few of those. – VLAZ Oct 24 '22 at 07:13
  • @deceze Greeting function is just and example i want to use for user login session. It check for user login session and i want to logout user automatically after 35 days – Ahsan Ullah Oct 24 '22 at 07:14
  • 4
    That is a weirdly specific, long time, and should probably be better handled by having the intrinsic session token or whatever have a built in expiry timestamp… – deceze Oct 24 '22 at 07:15
  • 2
    @AhsanUllah if a user navigates away from the page, that timer will be destroyed. It doesn't run in the background. There are workers that can operate in the background but if you want to destroy a user session, that's completely inadequate - you need to do this on the server side. And for this you likely don't need to write any code at all, timed sessions are a technology known for the past several decades. There should be a ready-made implementation where you just say "I want a timed session of X length" and that's it. – VLAZ Oct 24 '22 at 07:16
  • @VLAZ i appreciate your answer thanks for the clarification. – Ahsan Ullah Oct 24 '22 at 07:18
  • 4
    In general: starting a process (here: `setTimeout`) and expecting it to run continuously without interruption for anything more than a few minutes in an environment you have zero control over is… very optimistic. You must always adopt an approach as if your program will be terminated without warning at any second; because it will be. – deceze Oct 24 '22 at 07:22

1 Answers1

2

Here is your answer:

https://developer.mozilla.org/en-US/docs/Web/API/setTimeout#maximum_delay_value

Browsers including Internet Explorer, Chrome, Safari, and Firefox store the delay as a 32-bit signed integer internally. This causes an integer overflow when using delays larger than 2,147,483,647 ms (about 24.8 days), resulting in the timeout being executed immediately.

alex3683
  • 1,460
  • 14
  • 25