Skip to content

Commit

Permalink
Add guards to possibly null timeout IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
colinrotherham committed Oct 17, 2023
1 parent e0143b8 commit 8d85f0a
Showing 1 changed file with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -292,15 +292,15 @@ export class ExitThisPage extends GOVUKFrontendComponent {
this.updateIndicator()

// Clear the timeout for the keypress timeout message clearing itself
if (this.timeoutMessageId !== null) {
if (this.timeoutMessageId) {
window.clearTimeout(this.timeoutMessageId)
this.timeoutMessageId = null
}

if (this.keypressCounter >= 3) {
this.keypressCounter = 0

if (this.keypressTimeoutId !== null) {
if (this.keypressTimeoutId) {
window.clearTimeout(this.keypressTimeoutId)
this.keypressTimeoutId = null
}
Expand All @@ -315,7 +315,7 @@ export class ExitThisPage extends GOVUKFrontendComponent {
}

this.setKeypressTimer()
} else if (this.keypressTimeoutId !== null) {
} else if (this.keypressTimeoutId) {
// If the user pressed any key other than 'Shift', after having pressed
// 'Shift' and activating the timer, stop and reset the timer.
this.resetKeypressTimer()
Expand All @@ -338,7 +338,9 @@ export class ExitThisPage extends GOVUKFrontendComponent {
setKeypressTimer() {
// Clear any existing timeout. This is so only one timer is running even if
// there are multiple keypresses in quick succession.
window.clearTimeout(this.keypressTimeoutId)
if (this.keypressTimeoutId) {
window.clearTimeout(this.keypressTimeoutId)
}

// Set a fresh timeout
this.keypressTimeoutId = window.setTimeout(
Expand All @@ -357,8 +359,10 @@ export class ExitThisPage extends GOVUKFrontendComponent {
return
}

window.clearTimeout(this.keypressTimeoutId)
this.keypressTimeoutId = null
if (this.keypressTimeoutId) {
window.clearTimeout(this.keypressTimeoutId)
this.keypressTimeoutId = null
}

const $updateSpan = this.$updateSpan

Expand Down

0 comments on commit 8d85f0a

Please sign in to comment.