我有一个启动时间的关键活动,但我不理解为什么要停止关键工作。 它正在按预期开展工作,但我对其工作原因有疑虑。 为什么时间推移到关键缩编? 我没有在关键活动听众的任何地方叫停Watch。 我认为,我已经经历过一切可能的情况,但我仍感不到。
let cubeTimer = document.getElementById("cubeTimer");
let appendMinutes = document.getElementById("minutes");
let appendSeconds = document.getElementById("seconds");
let appendMilliseconds = document.getElementById("milliseconds");
let scramble = document.getElementById("scramble");
let minutes = 0;
let seconds = 0;
let milliseconds = 0.00;
let start = true;
let stop = false;
let arr = [];
let interval;
let scrambleArray = ["R", "L", "R ", "L ", "U", "D", "U ", "D ", "F", "B", "F ", "B ", "R2", "L2", "U2", "B2", "D2", "F2"];
function scrambleCube () {
let newArr = []
for (let i = 0; i < 21; i++) {
if (newArr.length >= 1) {
newArr.push(scrambleArray[Math.floor(Math.random() * 18)]);
do {
newArr[i] = scrambleArray[Math.floor(Math.random() * 18)]
} while (newArr[i][0] === newArr[i - 1][0]);
};
if (newArr.length < 1) {
newArr.push(scrambleArray[Math.floor(Math.random() * 18)]);
};
};
for (let i = 0; i < newArr.length; i++) {
newArr[i] = " " + newArr[i]
};
scramble.innerHTML = newArr;
//console.log(newArr)
}
scrambleCube()
document.addEventListener("keydown", () => {
arr.push("a");
if (arr.length > 10) {
cubeTimer.style.color = "green";
cubeTimer.style.fontSize = "40px";
start = true;
stop = false;
minutes = 0;
seconds = 0;
milliseconds = 0.00;
appendMinutes.innerHTML = "00";
appendSeconds.innerHTML = "00";
appendMilliseconds.innerHTML = "00";
} else if (arr.length < 10) {
cubeTimer.style.color = "red";
}
if (start === false) { stop = true };
});
document.addEventListener("keyup", () => {
start = false;
cubeTimer.style.color = "green";
if (arr.length > 10) {
interval = setInterval(stopWatch, 10);
}
arr = [];
});
function stopWatch () {
if (stop === true) {
clearInterval(interval);
scrambleCube()
};
milliseconds++;
if (milliseconds == 100) {
seconds++;
milliseconds = 0;
};
if (seconds == 60) {
minutes++;
seconds = 0;
};
let milliString = milliseconds;
let secondString = seconds;
let minuteString = minutes;
if (minutes < 10) {
minuteString = "0" + minuteString;
};
if (seconds < 10) {
secondString = "0" + secondString;
};
if (milliseconds < 10) {
milliString = "0" + milliString;
};
appendMinutes.innerHTML = minuteString;
appendSeconds.innerHTML = secondString;
appendMilliseconds.innerHTML = milliString;
};