[英]How to make a timer which doesnt start dircetly?
So i want to make a rubiks cube timer (example cstimer.net).所以我想制作一个魔方计时器(例如 cstimer.net)。
It's just like you hold space for a bit, you can release and start.就像你保持空间一会儿,你可以释放并开始。
but if you press space and release instantly it wont start.但是如果你按下空格并立即释放它就不会启动。
look at the website to understand what i mean.查看网站以了解我的意思。
but i dont understand how i can do it because i've tried some things but i cant figure it out!但我不明白我该怎么做,因为我尝试了一些东西,但我无法弄清楚!
this is what i would do but i dont know more这就是我会做的,但我不知道更多
document.addEventListener("keydown", (e) => {
if (e.keyCode === 32) {
spacedown = true;
}
})
document.addEventListener("keyup", (e) => {
if (e.keyCode === 32) {
spacedown = false;
}
})```
I think I found it out, The code is something like this我想我发现了,代码是这样的
let spacedown = false;
let spaceddown = false;
let ready = false;
document.addEventListener("keydown", (e) => {
if (e.keyCode === 32) {
spacedown = true;
if (!spaceddown) {
console.log('keydown & !spaceddown');
start();
}
spaceddown = true;
}
});
document.addEventListener("keyup", (e) => {
if (e.keyCode === 32) {
console.log('keyup');
spacedown = false;
spaceddown = false;
start();
ready = false;
}
});
function start() {
if (ready) {
console.log('REadyyy'),
timer.style.backgroundColor = 'white';
startTimer();
return
}
if (!spacedown) {
console.log('not space'),
timer.style.backgroundColor = 'white';
return
}
setTimeout(() => {
if (spacedown) {
console.log('yellow');
timer.style.backgroundColor = 'yellow';
}
}, 500)
setTimeout(() => {
if (spacedown) {
console.log('red');
timer.style.backgroundColor = 'red';
}
}, 1000)
setTimeout(() => {
if (spacedown) {
ready = true;
console.log('green');
timer.style.backgroundColor = 'green';
}
}, 1500)
}
function startTimer(){
//your timer code
}```
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.