Merge pull request #352 from typerqeo:fix-time

fixed time drift by using a self-adjusting timer
This commit is contained in:
Jack 2020-09-11 13:37:44 +01:00 committed by GitHub
commit e488014f1a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -6,7 +6,7 @@ let correctedHistory = [];
let currentCorrected = "";
let currentInput = "";
let time = 0;
let timers = [];
let timer = null;
let testActive = false;
let testStart, testEnd;
let wpmHistory = [];
@ -1633,7 +1633,7 @@ function showResult(difficultyFailed = false) {
correctSpaces: 0,
};
}
clearIntervals();
clearTimeout(timer);
let testtime = stats.time;
let afkseconds = keypressPerSecond.filter((x) => x.count == 0).length;
let afkSecondsPercent = roundTo2((afkseconds / testtime) * 100);
@ -2322,7 +2322,7 @@ function startTest() {
// }
// updateActiveElement();
updateTimer();
clearIntervals();
clearTimeout(timer);
keypressStats = {
spacing: {
current: -1,
@ -2333,8 +2333,11 @@ function startTest() {
array: [],
},
};
timers.push(
setInterval(function () {
//use a recursive self-adjusting timer to avoid time drift
const stepIntervalMS = 1000;
(function loop(expectedStepEnd) {
const delay = expectedStepEnd - Date.now();
timer = setTimeout(function () {
time++;
if (config.mode === "time") {
updateTimer();
@ -2412,16 +2415,18 @@ function startTest() {
if (config.mode == "time") {
if (time >= config.time) {
//times up
clearIntervals();
clearTimeout(timer);
hideCaret();
testActive = false;
inputHistory.push(currentInput);
correctedHistory.push(currentCorrected);
showResult();
return;
}
}
}, 1000)
);
loop(expectedStepEnd + stepIntervalMS);
}, delay);
})(testStart + stepIntervalMS);
}
function restartTest(withSameWordset = false) {
@ -2447,7 +2452,7 @@ function restartTest(withSameWordset = false) {
}
manualRestart = false;
clearIntervals();
clearTimeout(timer);
time = 0;
// afkDetected = false;
wpmHistory = [];
@ -2588,7 +2593,7 @@ function restartTest(withSameWordset = false) {
125,
() => {
hideCrown();
clearIntervals();
clearTimeout(timer);
$("#restartTestButton").css("opacity", 1);
if ($("#commandLineWrapper").hasClass("hidden")) focusWords();
wpmOverTimeChart.options.annotation.annotations[0].value = "-30";
@ -2903,12 +2908,6 @@ function swapElements(
}
}
function clearIntervals() {
timers.forEach((timer) => {
clearInterval(timer);
});
}
function updateAccountLoginButton() {
if (firebase.auth().currentUser != null) {
swapElements(