keylistener没有'一开始不起作用
keylistener doesn't work at the first
我的程序就像一个反应游戏。当按下"向上箭头"时,背景颜色变为红色。你需要在2秒内按下按钮。如果你不按下,那么你就输了,否则游戏正在进行。但在前2秒,你无论如何都输了,但游戏进行得很好。那么,前2秒的代码有什么问题呢?
我的代码:
var ido = 2000;
var nyomott = 0;
var nemelso = 0;
// >> main()
function main() {
nyomott = 0;
var r = Math.floor(Math.random() * 4);
var szin;
switch (r) {
case 0:
szin = "red";
break;
case 1:
szin = "green";
break;
case 2:
szin = "yellow";
break;
case 3:
szin = "blue";
break;
}
var print = "<h1>" + "Pess the key: " + "</br>" + szin + "</h1>" + "</br>";
document.getElementById("results").innerHTML = print;
startTimer();
}
var ciklus = setInterval(startTimer, ido);
// >> startTimer() : This function starts the timer
function startTimer() {
timerId = setTimeout(function() {
if (nyomott == 0) {
document.getElementById("results").innerHTML = "<h1>Lose</h1>";
clearInterval(ciklus);
} else {
main();
}
}, ido);
}
document.addEventListener("keydown", function(inEvent) {
if (inEvent.keyCode == 38) {
document.body.style.backgroundColor = "red";
nyomott = 1;
console.log(nyomott);
} else if (inEvent.keyCode == 404) document.body.style.backgroundColor = "green";
else if (inEvent.keyCode == 405) document.body.style.backgroundColor = "yellow";
else if (inEvent.keyCode == 406) document.body.style.backgroundColor = "blue";
});
body {
width: 100%;
height: 100%;
background-color: #202020;
}
div {
position: absolute;
height: 100%;
width: 100%;
display: table;
font-size: 60px;
color: #ffffff;
}
h1 {
display: table-cell;
vertical-align: middle;
text-align: center;
color: #FFFFFF;
}
<div onload="main()" id="results">
<h1></h1>
</div>
问题是在function main()
中,您设置了满足打印"丢失"的function startTimer()
中的条件的nyomott = 0;
。
进行了一些调整
//pushed your variables up to be above the event listener function to be able to set nyomotto = 1
var ido = 2000;
var nyomott = 0;
var nemelso = 0;
document.addEventListener("keydown", function(inEvent) {
if (inEvent.keyCode == 38) {
document.body.style.backgroundColor = "red";
nyomott = 1;
console.log(nyomott);
} else if (inEvent.keyCode == 404)
document.body.style.backgroundColor = "green";
else if (inEvent.keyCode == 405)
document.body.style.backgroundColor = "yellow";
else if (inEvent.keyCode == 406)
document.body.style.backgroundColor = "blue";
});
function main() {
var r = Math.floor(Math.random() * 4);
var szin;
switch (r) {
case 0:
szin = "red";
break;
case 1:
szin = "green";
break;
case 2:
szin = "yellow";
break;
case 3:
szin = "blue";
break;
}
var print = "<h1>" + "Pess the key: " + "</br>" + szin + "</h1>" + "</br>";
document.getElementById("results").innerHTML = print;
startTimer();
}
var ciklus = setInterval(startTimer, ido);
// This function starts the timer
function startTimer() {
timerId = setTimeout(function() {
if (nyomott == 0) {
document.getElementById("results").innerHTML = "<h1>Lose</h1>";
clearInterval(ciklus);
} else {
main();
}
}, ido);
}
body {
width: 100%;
height: 100%;
background-color: #202020;
}
div {
position: absolute;
height: 100%;
width: 100%;
display: table;
font-size: 60px;
color: #ffffff;
}
h1 {
display: table-cell;
vertical-align: middle;
text-align: center;
color: #FFFFFF;
}
<div onload="main()" id="results">
<h1></h1>
</div>
相关文章:
- 我正在创建一个聊天,但每次我发送消息时,它都不会让我再发送另一条消息,就像表格一样;不起作用
- 一键启动按钮不起作用
- 一页签出在Magento版本1.7.0.2中不起作用
- 角度限制过滤器's的第二参数“s”;“开始”;在Angular 1.3中似乎不起作用
- 复选框一次选中一个不起作用
- Angular 1.5变量在调用一次之前不起作用
- 我需要一种自动调整iframe大小的方法,无论我查到什么,它都不起作用
- 按下空格键一次后,Jquery功能不起作用
- 我有一系列的处方受试者(a->B->C->a),但最后一步不起作用
- CSS悬停在JQuery循环的最后一条记录中不起作用
- 一页上的多个数据表不起作用
- Javascript.我一添加math.floor函数,calc()函数就不起作用了
- jBox模态对话框Created监听器函数从第二次开始就不起作用了
- Jquery.Click() 在使用一次后不起作用
- 整页.JS一页滚动在手机上不起作用
- 引导轮播不起作用(一次显示所有图像)
- 数据表每 30 秒自动刷新一次不起作用
- 下拉菜单不会显示超过一秒钟.不起作用
- 谷歌脚本每分钟触发一次不起作用
- keylistener没有'一开始不起作用