From fc49bcbea3ed6efa94d393df8500714ed8b1f8df Mon Sep 17 00:00:00 2001 From: Brian Lai Date: Thu, 22 Dec 2022 15:02:21 -0500 Subject: [PATCH] Compensate for setTimeout lag --- client/singleplayer/tossups.js | 10 +++++----- server/Room.js | 14 +++++++------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/client/singleplayer/tossups.js b/client/singleplayer/tossups.js index b9dc90619..53e50ef9c 100644 --- a/client/singleplayer/tossups.js +++ b/client/singleplayer/tossups.js @@ -173,7 +173,7 @@ async function next() { document.getElementById('set-name-info').innerHTML = setName; paused = false; - readQuestion(); + readQuestion(new Date().getTime()); } @@ -184,7 +184,7 @@ function pause() { if (paused) { document.getElementById('buzz').removeAttribute('disabled'); document.getElementById('pause').innerHTML = 'Pause'; - readQuestion(); + readQuestion(new Date().getTime()); } else { document.getElementById('buzz').setAttribute('disabled', 'disabled'); @@ -198,7 +198,7 @@ function pause() { /** * Recursively reads the question based on the reading speed. */ -function readQuestion() { +function readQuestion(expectedReadTime) { if (!currentlyBuzzing && questionTextSplit.length > 0) { const word = questionTextSplit.shift(); document.getElementById('question').innerHTML += word + ' '; @@ -214,8 +214,8 @@ function readQuestion() { time = 0; timeoutID = window.setTimeout(() => { - readQuestion(); - }, time * 0.9 * (125 - document.getElementById('reading-speed').value)); + readQuestion(time * 0.9 * (125 - document.getElementById('reading-speed').value) + expectedReadTime); + }, time * 0.9 * (125 - document.getElementById('reading-speed').value) - new Date().getTime() + expectedReadTime); } else { document.getElementById('pause').disabled = true; } diff --git a/server/Room.js b/server/Room.js index a6e3f8aaa..17ab17788 100644 --- a/server/Room.js +++ b/server/Room.js @@ -53,7 +53,7 @@ class Room { if (this.buzzedIn === userId) { this.buzzedIn = null; this.players[userId].updateStats(-5, 0); - this.readQuestion(); + this.readQuestion(new Date().getTime()); this.sendSocketMessage({ type: 'give-answer', userId: userId, @@ -367,7 +367,7 @@ class Room { this.players[userId].updateStats(points, celerity); Object.values(this.players).forEach(player => { player.tuh++; }); } else if (directive === 'reject') { - this.readQuestion(); + this.readQuestion(new Date().getTime()); this.players[userId].updateStats(points, celerity); } @@ -402,7 +402,7 @@ class Room { username: this.players[userId].username, tossup: this.tossup }); - this.readQuestion(); + this.readQuestion(new Date().getTime()); } pause(userId) { @@ -411,7 +411,7 @@ class Room { if (this.paused) { clearTimeout(this.timeoutID); } else { - this.readQuestion(); + this.readQuestion(new Date().getTime()); } this.sendSocketMessage({ @@ -421,7 +421,7 @@ class Room { }); } - readQuestion() { + readQuestion(expectedReadTime) { if (Object.keys(this.tossup).length === 0) return; if (this.wordIndex >= this.questionSplit.length) { return; @@ -446,8 +446,8 @@ class Room { }); this.timeoutID = setTimeout(() => { - this.readQuestion(); - }, time * 0.9 * (125 - this.settings.readingSpeed)); + this.readQuestion(time * 0.9 * (125 - this.settings.readingSpeed) + expectedReadTime); + }, time * 0.9 * (125 - this.settings.readingSpeed) - new Date().getTime() + expectedReadTime); } revealQuestion() {