setInterval运行一次节点函数并继续运行但不运行函数

时间:2015-07-31 23:16:16

标签: javascript node.js setinterval

我正在运行一个调用节点模块的节点进程

var monitor = require('./monitor.js');

var monitorInterval = setInterval(function () {
    monitor();
    console.log('running monitor')
}, 10000);

第一次运行监视器模块,但它似乎在每个后续10秒间隔运行间隔但不运行该功能。日志看起来像这样。

MONITOR PROCESS STARTING
running monitor
running monitor
database connected
database connected
etc...

running monitor
running monitor

running monitor
running monitor
<10s PASS AND MONITOR DOES NOT RUN HERE>

running monitor
running monitor
<10s PASS MONITOR DOES NOT RUN HERE>

那为什么呢?可能是因为JS引擎没有闲置?我很确定它在后续的时间间隔内什么都不做。

监视器初始化如下:

function Monitor() {
    this._initialize();
}

module.exports = function (cb) {
    if (monitor === null) {
        monitor = new Monitor();
    }
};

Monitor.prototype = {

    _initialize: function () {
        console.log('MONITOR PROCESS STARTING');
        var monitor = this;
//etc
    }
}

0 个答案:

没有答案