我确定案件在这里有效,并且每次都可以审理案件。基本上我想在爆发之前通过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');
});
我希望它遵循这种模式:
努力解决这个问题,看看可能是一个功能?但即使这样,它也能很快执行这项功能。
由于
答案 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));