Cancel previous timeout
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user