Skip to content

Countdown Timer has buggy setTimeout logic #81

@EnoughTea

Description

@EnoughTea

I'm submitting a ...

[X] bug report

Steps to reproduce the bug:
It's a countdown timer bug. Timer is using setTimeout function repeatedly from recursive function without checking if timeout was previously set. So if user inadvertently calls timerTick several times, for example, by quickly alternating between calls to resumeTimer and pauseTimer, he can set several timeouts running in parallel, resulting in an incorrect timer.

Related code:
It can be fixed in several ways, for example, current function

timerTick() {
  setTimeout(() => {
  ...
  });
}

can be changed into something like

timerTick() {
  // this._timeoutHandle is null by default, so if it is not null now, means setTimeout() 
  // was previously called, and is now either executing or preparing to execute. 
  // By calling clearTimeout() in such case, we prevent another timerTick call chain
  // running in another timeout callback.
  if (this._timeoutHandle != null) {
    clearTimeout(this._timeoutHandle);
    this._timeoutHandle = null;
  }

  this._timeoutHandle = setTimeout(() => {
    ...
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions