30
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.

bryanmac
  • 38,941
  • 11
  • 91
  • 99
janiv
  • 749
  • 2
  • 6
  • 16

2 Answers2

1

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.

SUNDARRAJAN K
  • 2,237
  • 2
  • 22
  • 38
  • Can you explain why it helps? I've spoke with Janiv and he confirmed that your answer solved his issue. I'll give you the bounty afterward. – gdoron Jul 22 '15 at 20:45
  • 2
    I'm very familiar with asynchronous programming in javascript, but still don't get what's the difference between `setInterval(funcName, delay)` and `setInterval(function(){funcName()}, delay)` at least in browsers' javascript they are the same. – gdoron Jul 23 '15 at 07:11
  • 2
    Even if it works, this answer is wrong. The explanation is totally unrelated – vkurchatkin Jul 23 '15 at 20:51
  • vkurchatkin could you explain me the answer? – SUNDARRAJAN K Jul 24 '15 at 03:22
  • I can't. This is likely a bug in v8. – vkurchatkin Jul 24 '15 at 11:28
  • This doesn't answer the OP's question at all and is wrong. – Vivin Paliath Jul 24 '15 at 16:44
  • 1
    @SUNDARRAJANK: In javascript, every function is a closure weather it is anonymous or not. There should be no difference in behavior weather you wrap the function in another anonymous function or not since `setTimeout` is supposed to hold a reference to the function and prevent it from being garbage collected (indeed, named functions should never be garbage collected but anonymous function may be garbage collected). This is an obvious bug in node.js – slebetman Jul 24 '15 at 19:45
  • @slebetman Slight correction "In javascript, every function **that references a variable from an outer scope** is a closure whether it is anonymous or not". :) – Vivin Paliath Jul 24 '15 at 20:17
  • @VivinPaliath: All functions reference at least one variable from an outer scope (which is technically an outer scope). At the very least the global scope. So all functions are closures. – slebetman Jul 24 '15 at 20:42
  • @slebetman You're right; every function's context has an implicit reference to the global context whenever it is created. So every function is technically a closure (at least in JS). I think I conflated the general notion with JavaScript's realization of it. – Vivin Paliath Jul 24 '15 at 20:51
0

To me this looks like a non standard modual that you are using instead of the native modual. Look at global.process.moduleLoadList it should have the entries

 'NativeModule timers',
 'Binding timer_wrap',

And check your code that you are not importing 3rd party timers.js

Blindman67
  • 51,134
  • 11
  • 73
  • 136
  • I'm not importing third party timers.js. Where should I look for NativeModule and Binding? – janiv Jul 19 '15 at 23:39