let scramble = document.getElementById('scramble');
let timer = document.getElementById('timer');
const possibleMoves = ["R", "R'", "R2", "L", "L'", "L2", "U", "U'", "U2", "D", "D'", "D2", "F", "F'", "F2", "B", "B'", "B2"];
let newScramble = [];
let prevMove = '';
let prevBeforeMove = '';
let firstElOfPrevMove = '';
let firstElOfPrevBeforeMove = '';
const invalidPairs = [['R', 'L', 'R'], ['L', 'R', 'L'], ['F', 'B', 'F'], ['B', 'F', 'B'],
['D', 'U', 'D'], ['U', 'D', 'U']];
function setScramble() {
newScramble = [];
for (let i = 0; i <= 19; i++) {
prevMove = newScramble[i - 1];
prevBeforeMove = newScramble[i - 2];
prevMove && prevMove.length > 0 ? firstElOfPrevMove = prevMove[0] : null;
prevBeforeMove && prevBeforeMove.length > 0 ? firstElOfPrevBeforeMove = prevBeforeMove[0] : null;
do {
randomMove = possibleMoves[Math.floor(Math.random() * possibleMoves.length)];
} while (randomMove[0] === firstElOfPrevMove ||
invalidPairs.some(pair => pair[0] === randomMove[0] && pair[1] === firstElOfPrevMove && pair[2] === firstElOfPrevBeforeMove));
newScramble.push(randomMove);
scramble.textContent = newScramble.join(' ');
}
}
//---------------------TIMER--------------------
let hours = 0;
let minutes = 0;
let seconds = 0;
let milliseconds = 0;
let isStarted = false;
let interval;
function handleInterval(n) {
return (n < 10 ? '0' : '') + n;
}
function startTimer(event) {
if(event.code === 'Space' && !isStarted) {
milliseconds = 0;
seconds = 0;
minutes = 0;
isStarted = true;
interval = setInterval(() => {
milliseconds += 1;
if (minutes !== 0) {
timer.textContent = handleInterval(minutes) + ':' + handleInterval(seconds) + '.' + handleInterval(milliseconds);
} else {
timer.textContent = seconds + '.' + handleInterval(milliseconds);
}
if(milliseconds >= 99) {
seconds++;
milliseconds = 0;
}
if(seconds >= 60) {
seconds = 0;
minutes++;
}
}, 10);
} else {
isStarted = false;
}
}
function stopTimer(event) {
if(milliseconds >= 1 && event.code === 'Space' && isStarted) {
clearInterval(interval);
setScramble();
}
}
document.addEventListener('keyup', startTimer);
document.addEventListener('keydown', stopTimer);
setScramble();
Okay so I have a quick question. I made a website that times your rubiks cube solves and gives you a scramble (something like cstimer.net). But when I started to compare the time on my website with normal stopwatch I noticed that on firefox my time was like 1.5x slower. Then I tried the same thing on Microsoft Edge and it was working just fine.
So my question is: Why does that happen and what’s the best way to fix the problem?
I assume I won’t achieve my goal with setInterval + I want the website to work on all browsers (obviously).
Gabriel Szapałowski is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1