Cancel previous timeout

This commit is contained in:
2023-11-19 12:39:00 +01:00
parent b5ec58b936
commit 90400a09ea

View File

@@ -8,6 +8,9 @@ const display = /** @type {HTMLDivElement} */ (document.getElementById('display'
const light = /** @type {HTMLDivElement} */ (document.getElementById('light'));
const keypad = /** @type {HTMLDivElement} */ (document.getElementById('keypad'));
/** @type {number|null} */
let taskId = null;
/**
* @param {number} len
* @returns {string}
@@ -30,6 +33,10 @@ function resetCode() {
* @param {number} digit
*/
function composeDigit(digit) {
if (taskId !== null) {
clearTimeout(taskId);
taskId = null;
}
builder += digit.toString();
display.innerText = builder + '_'.repeat(CODE.length - builder.length);
light.classList.remove(IS_SUCCESS, IS_ERROR);
@@ -37,7 +44,7 @@ function composeDigit(digit) {
light.classList.add(IS_SUCCESS);
} else {
light.classList.add(IS_ERROR);
setTimeout(resetCode, DELAY);
taskId = setTimeout(resetCode, DELAY);
}
}