简体   繁体   中英

NodeJS setInterval timer crashing after first interval

I am trying to execute a function every hour. This essentially validates documents from a MonoDB. However, at time of first interval the application crashes throwing the following error:

timers.js:252  
 callback.apply(this, args);  
          ^  

TypeError: Cannot call method 'apply' of undefined  
    at wrapper [as _onTimeout] (timers.js:252:14)
    at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)  

Here are snippets from my files:

validator

function validate() { ... }

module.exports = {
  validate : validate
}

www

var validator = require('../lib/validator');
var app = require('../app');

app.listen(3000, function() {
  setInterval(validator.validate(), 360000);
}

setInterval() takes a method reference. You only need to remove the parentheses from where you pass in the callback:

setInterval(validator.validate, 360000);

Essentially, the second time the interval fires it would be trying to invoke the result of your first call to validate() instead of the function itself, thus the error. Here's a similar test in client-side JavaScript (setInterval behaves the same way to my knowledge):

 function setTime() { $('#currentDateTime').html(new Date()); } $(function() { setInterval(setTime, 1000); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="currentDateTime"></div> 

ALSO As an interesting just in case to this answer, I found another user who appears to be passing the method reference in properly but getting a similar error. The comment supposes that maybe there could be an upper bound to the timeout being hit (size of int? signed int? just guessing).

Node.js crashes when using long interval in setinterval

You are not calling setInterval correctly... it should be wrapped in it's own function scope.

 app.listen(3000, function() { setInterval(function() { validator.validate() }, 360000); } 

Try the following snippet.

app.listen(3000, function() {
  setInterval(validator.validate.bind(validator), 360000);
}

setInterval takes a function as the first parameter, and your code snippet calls validator.validate() immediately (and I assume that function call does not return a function).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM