79126221

Date: 2024-10-25 14:58:01
Score: 2
Natty:
Report link

The clearTimeout(timeoutID) works because timeoutID is declared outside the updateMessage function, so it keeps its value across multiple calls of updateMessage. When clearTimeout(timeoutID) runs, it stops the previous timeout (if there was one), then a new setTimeout is started and assigned to timeoutID. This way, each time you press the button, it resets the timer by clearing the old timeout and starting a fresh 2-second countdown.

Reasons:
  • No code block (0.5):
  • Single line (0.5):
  • Low reputation (1):
Posted by: Sarfaraz Khatti