Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Countdown Timer has buggy setTimeout logic #81

Open
EnoughTea opened this issue Apr 10, 2018 · 0 comments
Open

Countdown Timer has buggy setTimeout logic #81

EnoughTea opened this issue Apr 10, 2018 · 0 comments

Comments

@EnoughTea
Copy link

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(() => {
    ...
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant