setTimeout在Node.js中等待太长时间 [英] setTimeout waits too long in Node.js

查看:98
本文介绍了setTimeout在Node.js中等待太长时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我对 setTimeout 在JavaScript中的确切功能有误解.我有这个脚本:

I think I have a misunderstanding of what setTimeout does exactly in JavaScript. I have this script:

function recursiveFibonacci(n) {
    if ( n === 0 ) {
        return 1;
    } else if ( n === 1 ) {
        return 1;
    } else {
        return recursiveFibonacci(n-1) + recursiveFibonacci(n-2);
    }
}

setTimeout( () => {console.log("one second");}, 1000);

console.log(recursiveFibonacci(42));

我希望发生的是,递归斐波那契开始在斐波那契序列中的第43个值上偏离.在我的计算机上,这大约需要4秒钟.因此,工作1秒后,评估将中断,控制台将记录:

What I would expect to happen is that recursiveFibonacci starts chugging away on the 43rd value in the Fibonacci sequence. This takes about 4 seconds on my computer. So, after 1 second of work the evaluation would be interrupted and the console would log:

one second

,然后大约3秒后登录:

and then about 3 seconds later log:

433494437

相反,在4秒钟后,控制台将记录日志:

Instead what happens is that, after 4 seconds, the console logs:

433494437
one second

一次全部.为什么会这样呢?如何使 setTimeout 正常工作?是不是JavaScript解释器实际上没有被 setTimeout 中断,而是如果它完成了其他工作,那么它将等待直到经过给定的时间后才调用给定的功能?

all at once. Why is this the case? How can I get setTimeout to work? Is it the case that the JavaScript interpreter is not actually interrupted by setTimeout but rather if it finishes its other jobs then it will wait until the given amount of time has passed before calling the given function?

我发现此工具对于理解相关概念非常有用:

I found this tool very useful for understanding the relevant concepts:

放大镜

推荐答案

node.js中的Javascript是事件驱动的单线程.由于您的 reverseFibonacci()函数是同步的,因此在完成之前它不允许任何其他操作.

Javascript in node.js is event driven and single threaded. Since your reverseFibonacci() function is synchronous, it does not allow anything else to run until it is done.

因此,即使计时器已触发并且计时器回调已放入内部nodejs事件队列中,JS解释器也无法到达事件队列中的下一个事件,直到您的 reverseFibonacci()功能就完成了.

So, even though the timer fires and the timer callback is put into the internal nodejs event queue, the JS interpreter can't get to the next event in the event queue until your reverseFibonacci() function is done.

setTimeout()不会中断当前正在运行的Javascript.相反,它将事件放入事件队列中,当当前运行的Javascript完成后,JS解释器将把下一个事件从事件队列中拉出并运行.

setTimeout() does not interrupt the currently running Javascript. Instead, it puts an event in the event queue and when the currently running Javascript is done, the JS interpreter will then pull the next event out of the event queue and run it.

因此,您的方案中的事件顺序是这样的:

So, the order of events in your scenario is this:

  1. 从现在开始安排一个计时器事件1秒.
  2. 开始运行 reverseFibonacci()
  3. 计时器会在1秒后触发(内部在nodejs中触发),然后将一个事件插入到nodejs事件队列中.
  4. 4秒钟后,您的 reverseFibonacci(42)完成运行.
  5. 由于当前正在运行的JS已完成,因此JS解释器会将下一个事件从事件队列中拉出并进行处理,从而最终调用您的计时器回调.

使用 setTimeout()的计时器计划会在其计划时间之后尽快运行,但是如果Javascript解释器正忙于运行其他代码,则它们不会中断该代码.计时器系统将一个事件放入事件队列中,当其他代码完成后,JavaScript解释器将从事件队列中拉出下一个事件并运行它(您的计时器回调将被调用).

Timers schedule with setTimeout() runs as soon as possible after their scheduled time, but if the Javascript interpreter is busy running other code, they don't interrupt that code. The timer system puts an event into the event queue and, when that other code is done, then the Javascript interpreter will pull the next event out of the event queue and run it (your timer callback will get called).

这篇关于setTimeout在Node.js中等待太长时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆