事件发射器JavaScript

事件发射器JavaScript,javascript,node.js,eventemitter,Javascript,Node.js,Eventemitter,此代码块发出一个事件并传递一个整数: function executeEvent() { // here you could use the event bus to chuck the pfio event to the pfio event api..... if (eventQueue.length > 0) { for (var i = 0; i < eventQueue.length; i++) { var even

此代码块发出一个事件并传递一个整数:

function executeEvent() {
    // here you could use the event bus to chuck the pfio event to the pfio event api.....

    if (eventQueue.length > 0) {
        for (var i = 0; i < eventQueue.length; i++) {
            var event = eventQueue[i];

            if (event.processing){
                console.log(colors.yellow("Already processing event"));
                continue;
            }
            else
                event.processing = true;

            eventBus.emit('event.process', i);
        }
    }

    setImmediate(executeEvent.bind(this));
};
索引是
NaN
。有人能告诉我错误在哪里吗?我猜它可能在事件总线的
事件中。

更改

console.log("Processing %s %d", colors.yellow("Event"), colors.red(index.toString()));

虽然
index
在进入函数时是一个整数,但是
colors.red(index.toString())
的返回值不再是整数,并且%d替换字符串将尝试转换为整数,从而导致NaN。

我们都有这样的日子:)如果这对您有帮助,请将我的答案标记为正确,如果您愿意,请投票:)
console.log("Processing %s %d", colors.yellow("Event"), colors.red(index.toString()));
console.log("Processing %s %s", colors.yellow("Event"), colors.red(index.toString()));