Anki/qt/aqt/data/web/js/reviewer-bottom.ts
Antoine Q 32c129656e
Improve timer interval display (#3096) (#3100)
* Improve update interval of timer display

Timer calculation frequency increased and aligned with new card apparition.

* Update CONTRIBUTORS

* Update timer interval to 0.333 sec

* Restore timer to 1 sec

- Restoring timer to 1sec value as it is not necessary to increase the frequency.

- Adding a time update directy after new card is displayed so that the timer display immediatly restarts to 0.
2024-03-31 07:49:16 +01:00

63 lines
1.5 KiB
TypeScript

/* Copyright: Ankitects Pty Ltd and contributors
* License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html */
/* eslint
@typescript-eslint/no-unused-vars: "off",
*/
let time: number; // set in python code
let timerStopped = false;
let maxTime = 0;
function updateTime(): void {
const timeNode = document.getElementById("time");
if (maxTime === 0) {
timeNode.textContent = "";
return;
}
time = Math.min(maxTime, time);
const m = Math.floor(time / 60);
const s = time % 60;
const sStr = String(s).padStart(2, "0");
const timeString = `${m}:${sStr}`;
if (maxTime === time) {
timeNode.innerHTML = `<font color=red>${timeString}</font>`;
} else {
timeNode.textContent = timeString;
}
}
let intervalId: number | undefined;
function showQuestion(txt: string, maxTime_: number): void {
showAnswer(txt);
time = 0;
maxTime = maxTime_;
updateTime();
if (intervalId !== undefined) {
clearInterval(intervalId);
}
intervalId = setInterval(function() {
if (!timerStopped) {
time += 1;
updateTime();
}
}, 1000);
}
function showAnswer(txt: string, stopTimer = false): void {
document.getElementById("middle").innerHTML = txt;
timerStopped = stopTimer;
}
function selectedAnswerButton(): string {
const node = document.activeElement as HTMLElement;
if (!node) {
return;
}
return node.dataset.ease;
}