事件发射器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()));