Javascript 节点套接字io定时器的实现
我正在node&socket中创建一个绘图/猜测游戏。 简单地说: 我有一个班级房间,游戏(扩展房间)和一个班级回合(每场10场)。 每轮分配一个用户作为抽屉,猜测者有45秒的时间来猜测单词 根据第一次猜测,如果计时器高于20秒,则计时器将减少到20秒 我不确定,但我是这样开始的: 班级轮次:Javascript 节点套接字io定时器的实现,javascript,node.js,socket.io,socket.io-1.0,Javascript,Node.js,Socket.io,Socket.io 1.0,我正在node&socket中创建一个绘图/猜测游戏。 简单地说: 我有一个班级房间,游戏(扩展房间)和一个班级回合(每场10场)。 每轮分配一个用户作为抽屉,猜测者有45秒的时间来猜测单词 根据第一次猜测,如果计时器高于20秒,则计时器将减少到20秒 我不确定,但我是这样开始的: 班级轮次: function Round(id, game){ var ROUNDTIME = 45, timer = new Date(); this.id = id; this.game
function Round(id, game){
var ROUNDTIME = 45,
timer = new Date();
this.id = id;
this.game = game;
this.endTime = timer.setSeconds(timer.getSeconds() + ROUNDTIME);
...
}
function Game(){
this.MAX_ROUNDS = 10;
this.rounds = [];
this.currentRound = null;
...
}
Game.prototype.start = function(){
this.nextRound;
}
Game.prototype.nextRound = function(){
if(this.rounds.length <= this.MAX_ROUNDS){
this.currentRound = new Round(this.rounds.length + 1, this);
...
} else {
this.endGame();
}
}
Game.prototype.endGame = function(){
...
}
班级游戏:
function Round(id, game){
var ROUNDTIME = 45,
timer = new Date();
this.id = id;
this.game = game;
this.endTime = timer.setSeconds(timer.getSeconds() + ROUNDTIME);
...
}
function Game(){
this.MAX_ROUNDS = 10;
this.rounds = [];
this.currentRound = null;
...
}
Game.prototype.start = function(){
this.nextRound;
}
Game.prototype.nextRound = function(){
if(this.rounds.length <= this.MAX_ROUNDS){
this.currentRound = new Round(this.rounds.length + 1, this);
...
} else {
this.endGame();
}
}
Game.prototype.endGame = function(){
...
}
函数游戏(){
此最大轮数=10;
this.rounds=[];
this.currentRound=null;
...
}
Game.prototype.start=函数(){
这是下个月;
}
Game.prototype.nextRound=函数(){
如果(this.rounds.length您可以执行以下操作
function Round(id, game,loseCallbackFn){
var ROUNDTIME = 45, startTime = new Date();
this.id = id;
this.game = game;
this.endTime = startTime.getTime()+45*1000;
this.timerId = setTimeout(loseCallbackFn,45*1000);
...
}
...
Round.onWrongGuess(){
if((this.endTime-(new Date()).getTime())>20*1000) {// he has more than 20s left
clearTimeout(this.timerId);
this.timerId = setTimeout(loseCallbackFn,20*1000);
...
}else{
loseCallbackFn();
}
}
...
嘿,mido22,谢谢你的回复。回调从哪里来?因为我给了game类(包含NextRund函数)作为参数。我用game类更新了我的问题。在这种情况下,你可以从参数中删除回调,并在回调函数的位置传递game.endGame
,(或者您在Game object中声明的任何其他函数来处理失败场景)对发出倒计时脚本更新的函数使用回调(与Game.endRound()结合使用)怎么样?或者这不是一个好的做法(将套接字功能插入类中)?@goowik我认为最好不要使用socket代码,一种方法是在socket代码端进行闭包,并在游戏对象中设置一个变量。