0

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

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));
};

但是,当它开始接收事件时:

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事件总线的情况下。

4

1 回答 1

2

改变

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

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

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

于 2014-04-03T13:34:34.777 回答