在setinterval中使用长间隔时,Node.js崩溃 [英] Node.js crashes when using long interval in setinterval

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

问题描述

function createSasTokenTimer() {    
    console.log("Hello");
}

setInterval(createSasTokenTimer, 3000000);

我运行此代码,50分钟后出现以下错误:

I run this code and after 50 minutes I get the following error:

Hello
timers.js:265
    callback.apply(this, args);
            ^
TypeError: Cannot read property 'apply' of undefined

    at wrapper [as _onTimeout] (timers.js:265:13)
    at Timer.listOnTimeout (timers.js:110:15)

间隔时间较短时( 2000000 例如),一切正常。

When the interval time is shorter (2000000 for example), everything works fine.

这是Node.js中的错误吗?

Is this a bug in Node.js?

更新:

操作系统: Windows ,Node.js版本: 0.12.4

OS: Windows, Node.js version: 0.12.4

当我只运行上面的代码时它工作正常,但是当它在我的应用程序中时,它确实会破坏,我无法指出我的代码的哪一部分会破坏它,因为它非常冗长而且没有任何东西看起来可疑。无论如何,当间隔时间缩短时,它就像我写的一样。

When I run only the code above it works fine, but it does break when it's inside my application, I can't point to which part of my code breaks it as it's very lengthy and nothing looks "suspicious". Anyway, when the interval is shorter it works as I wrote.

推荐答案

而不是直接调用函数将其放入回调中。

Instead of calling the Function Directly give it inside a callback.

function createSasTokenTimer() {    
  console.log("Hello");
}

setInterval(function(){
 createSasTokenTimer();
}, 3000000);

使用此方法,您将匿名函数传递给setInterval。它将在每个时间间隔调用此函数一次,在此示例中为3000000毫秒。

Using this method, you are passing an anonymous function to setInterval. It will call this function once per interval, which is 3000000 miliseconds in this example.

现在,您可以使用此代码。为了进一步理解,我建议研究匿名函数和闭包。

For now, you can probably just use this code. For further understanding, I suggest researching anonymous functions and closures.

希望这会有所帮助。

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

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