我有setTimeout函数,它需要在特定的毫秒后执行问候函数2958219351,但它直接执行函数,而不需要等待22958219351,但是当我特定的setTimeout功能具有5000毫秒时,它会等待5秒,然后执行函数。
以下是2958219351毫秒的代码
<!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>
下面是5000毫秒的代码
<!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>
我不知道为什么它不等待2958219351毫秒就直接执行setTimeout
函数
以下是您的答案:
https://developer.mozilla.org/en-US/docs/Web/API/setTimeout#maximum_delay_value
包括Internet Explorer、Chrome、Safari和Firefox在内的浏览器在内部将延迟存储为32位带符号整数。当使用大于2147483647毫秒(约24.8天(的延迟时,这会导致整数溢出,导致立即执行超时。