nodejs中的setInterval奇怪行为

setInterval weird behavior in nodejs

我想运行每秒执行一次函数,函数本身需要3秒来执行。结果是每个间隔以 <function execution time>*2+<setInterval delay>

的差异执行

我写了下面的示例代码:

var seconds = 3;

setInterval(
    function(){
            console.info(new Date().toString());
            var waitTill = new Date(new Date().getTime() + seconds * 1000);
            while(waitTill > new Date()){}
    },1000
);

并且每次迭代都如我在公式中所述:

Wed Jul 13 2016 09:49:07 GMT+0300 (IDT)
Wed Jul 13 2016 09:49:14 GMT+0300 (IDT)
Wed Jul 13 2016 09:49:21 GMT+0300 (IDT)
Wed Jul 13 2016 09:49:28 GMT+0300 (IDT)

文档未说明此行为。我认为的结果是无论间隔函数的执行需要多少时间,每次迭代都会在 1 秒后执行。

怎么回事?

如能提供有关此问题的任何信息,我们将不胜感激。

谢谢!

使用 Node.js 6.3.0

更新

在浏览器上试过这段代码...google chrome...这里间隔每3秒执行一次,这仍然很奇怪。

更新

感谢您的所有评论,最后一件事尚不清楚。 为什么在 NodeJS 中,当我将 setInterval() 设置为 1 秒时,函数执行需要 3 秒,为什么下一次执行是 7 秒而不是 4 秒甚至 3 秒。这对我来说似乎真的很奇怪。这是可以接受的行为吗?

the documentation doesn't state this behavior

NodeJS 的 documentation for setInterval 特性几乎没有说明它的行为,除了它会重复任务。

the results that I thought that will be is that each iteration will execute after 1 second no matter how much time the execution of the interval function takes

如果你的意思是你可以有重叠的执行,你不能在 NodeJS 中;它 运行 是您在单个线程上的代码。

如果您的意思是您希望每次迭代在最后一次完成后 运行 秒,那不是 setInterval 传统的工作方式。 setInterval 传统上至少有两种不同的行为,具体取决于您使用的实现方式:将下一次迭代安排在当前迭代的 开始 处,或者将其安排在 [=当前的42=]end。那只是在浏览器上。这是从 been standardized for browsers 开始的,但 NodeJS 不是浏览器,不需要以相同的方式工作。 (事实上​​,它也不是另一种方式:在浏览器上,setInterval 需要 return 一个数字;在 NodeJS 上,它 return 是一个对象。)记住计时器不是 JavaScript 的功能,它们是主机环境的功能。

相反,要在它之前完成后一秒(大约)再次达到 运行,请在函数末尾使用 setTimeout 来安排下一个到 运行一秒钟后。

重新编辑:

why in NodeJS, when I have setInterval() set to 1 second, and the function execution takes 3 seconds, why the next execution is 7 seconds instead of 4 seconds or even 3 seconds. that seems really strange behavior for me. is that an acceptable behavior ?

是的。这很奇怪和令人惊讶(在我看来),但是 NodeJS 决定了它自己的 setInterval 行为,所以它 可以接受 。在我的实验中(如下),它似乎测量了你的函数之前执行的时间,然后 add 到计时器长度,以便它在触发它之前是 lastExecutionLength + desiredInterval再次。这与浏览器规范明显不同,但同样,NodeJS 不是浏览器。

这是我的测试脚本:

let counter = 0;
let timeAtEndOfLastExecution = 0;
let timer = null;

function log(msg) {
    console.log(Date.now() + ": " + msg);
}

function tick() {
    let start = Date.now();
    if (timeAtEndOfLastExecution) {
        log("tick (" + (Date.now() - timeAtEndOfLastExecution) + "ms)");
    } else {
        log("tick");
    }
    if (++counter == 10) {
        clearInterval(timer);
    } else {
        let wait = 200 + (Math.floor(8 * Math.random()) * 100);
        log("waiting " + wait + "ms");
        let stopWaiting = Date.now() + wait;
        while (Date.now() < stopWaiting) {
            // busy wait
        }
        log("exiting callback after " + (Date.now() - start) + "ms");
        timeAtEndOfLastExecution = Date.now();
    }
}
timer = setInterval(tick, 200);

和示例 运行(使用 Node v6.2.2):

1468396730618: tick
1468396730619: waiting 400ms
1468396731020: exiting callback after 416ms
1468396731637: tick (617ms)
1468396731637: waiting 500ms
1468396732137: exiting callback after 500ms
1468396732837: tick (700ms)
1468396732837: waiting 900ms
1468396733737: exiting callback after 900ms
1468396734837: tick (1100ms)
1468396734837: waiting 300ms
1468396735137: exiting callback after 300ms
1468396735637: tick (500ms)
1468396735637: waiting 700ms
1468396736337: exiting callback after 700ms
1468396737237: tick (900ms)
1468396737237: waiting 800ms
1468396738037: exiting callback after 800ms
1468396739036: tick (999ms)
1468396739036: waiting 900ms
1468396739936: exiting callback after 900ms
1468396741036: tick (1100ms)
1468396741036: waiting 700ms
1468396741736: exiting callback after 700ms
1468396742636: tick (900ms)
1468396742636: waiting 200ms
1468396742836: exiting callback after 200ms
1468396743236: tick (400ms)

正如我们所见,它一直在等待上一次迭代的长度加上我给的间隔:

  • 第一次回调共耗时416ms;下一个在 returned
  • 后 617 毫秒开始
  • 第二次回调耗时500ms;下一个在 returned
  • 后 700 毫秒开始
  • 第三次回调900ms;下一个在 returned
  • 后 1100 毫秒开始