【发布时间】:2016-07-02 23:31:03
【问题描述】:
我正忙于学习 socket.io 并试图弄清楚如何让一些 JS 只有在两个用户都点击了他们的答案后才会触发。
我要启动的有问题的代码是:
// Random number function
function randomIntFromInterval(min,max){
return Math.floor(Math.random()*(max-min+1)+min);
}
// Check if correct + send to server
function correctAnswer() {
var correct = true;
socket.emit('playerCorrect', {answer: correct});
console.log(correct);
buttonRemover();
}
// Check if wrong + send to server
function incorrectAnswer () {
var wrong = false;
socket.emit('playerWrong', {answer: wrong});
buttonRemover();
}
socket.on ('updatePlayer', function (data) {
if (data.answer === true) {
console.log ('Player got it right! ' + data.answer);
}else if (data.answer === false) {
console.log ('Player got it wrong! ' + data.answer);
}
});
这些函数向服务器发送数据以让服务器知道答案是否正确。
socket.on('playerCorrect', function (data) {
io.sockets.emit('updatePlayer', data);
dataRequest();
});
socket.on('playerWrong', function (data) {
io.sockets.emit('updatePlayer', data);
dataRequest();
});
但是,我只希望在两个客户都单击一个选项时发生这种情况。有没有办法跟踪这个?
【问题讨论】:
标签: javascript jquery socket.io