等待if语句的设定量继续

时间:2015-06-10 11:29:11

标签: javascript node.js

我确定案件在这里有效,并且每次都可以审理案件。基本上我想在爆发之前通过5个命令(除非我做另一个强制它突破的命令)。

它使用node.js和socket.io在index.html页面上使用socket / io.emits。

以下是我目前在节点文件中的内容:

//Commands
client.addListener('chat', function (channel, user, message) {
    console.log(user.username + ': ' + message);
    var msg = message;
    var Answerstate = 0;

    if (msg.toLowerCase().indexOf("!ff") == 0) {
        var parts = msg.split(" ");
        if(parts.length > 1) {
            parts.shift();
            var player = parts.join(" ");
            client.say(channel, player + " wants to play Family Fortunes!");
        }
        if (Answerstate < 5) {
            if (msg.toLowerCase().indexOf("!answer") == 0) { 
                var parts = msg.split(" ");
                if(parts.length > 1) {
                    Answerstate++; //Increase State
                    parts.shift();
                    var answer = parts.join(" ");
                    io.emit('answer',{player, answer}); 
                    console.log('Going over to index.html | io.emit "answerState" ');
            }
        }
    }
});

index.html:

    player       = {};
    answer       = {};
    socket.on('answer', function (player, answer) {
        console.log('player');
        console.log('answer');
    });

我希望它遵循这种模式:

  1. 我执行!ff&#34; NAME&#34;
  2. 他们打字!回答&#34;答案&#34;
  3. 答案的状态保持不变,因为其他人可以在以后拿起它。这存储在变量answerState中,但在index.html页面中已更改,因为我打开了整个时间。
  4. 在index.html中我希望每次有人打字时都这样!回答%ANSWER%它会查询它是否正确,如果是的话。然后继续。如果没有,请退出。
  5. 努力解决这个问题,看看可能是一个功能?但即使这样,它也能很快执行这项功能。

    由于

1 个答案:

答案 0 :(得分:0)

修改

我可能误会了。如果您希望前者运行一次并在以后拾取您的变量 Answerstate 需要是全局的,否则每次运行代码时它都将被重新定义并设置为0。

如果 Answerstate&lt; 您可以定义您的功能并只调用它。 5

//Commands
//Define answerState as a global
var answerState = 0;
//Define your original function as a variable to insert as an argument to 
//client.addListener() and pass the answerState argument to it
var answerQuestion = function (channel, user, message, answerState) {

    console.log(user.username + ': ' + message);
    var msg = message;

    if (msg.toLowerCase().indexOf("!ff") == 0) {
        var parts = msg.split(" ");
        if(parts.length > 1) {
            parts.shift();
            var player = parts.join(" ");
            client.say(channel, player + " wants to play Family Fortunes!");
        }
        if (answerState < 5) {
           if (msg.toLowerCase().indexOf("!answer") == 0) { 
                var parts = msg.split(" ");
                if(parts.length > 1) {
                    parts.shift();
                    var answer = parts.join(" ");
                    io.emit('answer',{player, answer}); 
                    console.log('Going over to index.html | io.emit "answerState" ');
                    //if answer is correct break out of function
                    if (answer === correct) {
                        answerState = 0;
                        return;
                    } 
                    //call the function recursively until answerState = 5
                    else {
                        answerQuestion(channel, user, message, answerState++);
                    }
                }
            }
        }
    }
}
//add your listener and insert answerQuestion as the argument with answerState
client.addListener('chat', answerQuestion(channel, user, message, answerState));