Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
368 views
in Technique[技术] by (71.8m points)

javascript - Node.js crashes when using long interval in setinterval

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

setInterval(createSasTokenTimer, 3000000);

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)

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

Is this a bug in Node.js?


Update:

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

setInterval(function(){
 createSasTokenTimer();
}, 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.

Hope this helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...