我试图使用 Session.get() 和 Session.set() 来检查用户是否单击了按钮。如果实现此代码以检查会话是否已设置。
"click .alt": function(event,template){
if (Session.get("selected") === false){
var clicked = event.currentTarget;
clicked.className += " chosen";
var data = clicked.dataset;
console.log(data);
Session.set("selected", true);
if (data.corr == true){
console.log("Hi");
}
else{
console.log("Not Hi");
}
}
else{
console.log("session was set to true");
}
}
consol.log(data) 总是被打印,但即使 data.corr=true,“Hi”也会被更新。出于某种未知的原因,对我来说,这不起作用。希望有人可以帮助我。
更新 这将不起作用:
if (Session.get("selected") == false){
Session.set("selected", true);
var clicked = event.currentTarget;
if (clicked.dataset.corr == true){
//Recording the time and calculating some points
time = new Date;
time = time.getTime();
time = 3000-Math.floor((time -Session.get("startedTime"))/10 );
gameCol.insert({team:Session.get("teamnr"), points: time});
console.log("Correct answer");
}
else{
//0 points for wrong answer
gameCol.insert({team:Session.get("teamnr"), points: 0});
console.log("Wrong answer");
}
}
else{
console.log("You have answered");
}
这有效:
if (Session.get("selected") == false){
Session.set("selected", true);
var clicked = event.currentTarget;
if (clicked.dataset.corr == true){
//Recording the time and calculating some points
time = new Date;
time = time.getTime();
time = 3000-Math.floor((time -Session.get("startedTime"))/10 );
gameCol.insert({team:Session.get("teamnr"), points: time});
console.log("Correct answer");
}
else{
//0 points for wrong answer
//gameCol.insert({team:Session.get("teamnr"), points: 0});
console.log("Wrong answer");
}
}
else{
console.log("You have answered");
}
唯一的区别是在 db 中插入错误答案的行是 remover。见行 //gameCol.insert({team:Session.get("teamnr"), points: 0});
更新 1.1 将 if/else 更改为两个 if 反而有效
if (clicked.dataset.corr == true){
//Recording the time and calculating some points
time = new Date;
time = time.getTime();
time = 3000-Math.floor((time -Session.get("startedTime"))/10 );
gameCol.insert({team:Session.get("teamnr"), points: time});
console.log("Correct answer");
}
if(clicked.dataset.corr == false){
//0 points for wrong answer
gameCol.insert({team:Session.get("teamnr"), points: 0});
console.log("Wrong answer");
}