首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >EventEmitter JavaScript

EventEmitter JavaScript
EN

Stack Overflow用户
提问于 2014-04-03 12:58:37
回答 1查看 449关注 0票数 0

此代码块发出一个事件并通过一个整数:

代码语言:javascript
复制
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));
};

但是,当它接收到事件时:

代码语言:javascript
复制
eventBus.on('event.process', processEvent);

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

    var event = getEvent(index);

    var current = new Date().getTime(); // get the number of milliseconds from 1/1/1970

    var fireEvent = false;

    if (event.eventType == 2) // timer or interval respectively
        fireEvent = current - event.init.getTime() >= event.when;

    if (event.eventType == 3) // schedule
        fireEvent = current >= event.when;

    if (fireEvent)
        eventBus.emit('event.fire', event);
    else
        event.processing = false;
}

function getEvent(index){
    return eventQueue[index];
}

indexNaN。有人能告诉我错误在哪里吗?我猜它可能在事件总线的on事件中。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-03 13:34:34

变化

代码语言:javascript
复制
console.log("Processing %s %d", colors.yellow("Event"), colors.red(index.toString()));

代码语言:javascript
复制
console.log("Processing %s %s", colors.yellow("Event"), colors.red(index.toString()));

尽管index在输入函数时是整数,但colors.red(index.toString())的返回值不再是整数,%d替换字符串将尝试将类型转换为整数,从而导致NaN。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22838514

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档