Skip to content
Advertisement

React – How to fix very inconsistent disabled button countdown (i.e., several whole seconds)

I’m using setTimeout() (I even tried driftless too) to change buttons from disabled = true to disabled = false 3 seconds after appearing on the DOM (onLoad=). The buttons appear in sequence, after the previous button has been clicked.

The problem is, the setTimeout() goes absolutely haywire: Each button’s disabled = true duration is seemingly sporadic (at least on Windows Chrome on 2 different PC’s). Sometimes the button seemingly waits the full 3 seconds to enable, sometimes it enables instantaneously, and everything in-between.

What is causing this inconsistency, and how can I get each button to enable exactly 3 seconds (well, within a couple hundred milliseconds…not a big deal) after appearing?

CodePen

Javascript

JavaScript

HTML

JavaScript

Advertisement

Answer

You should fix this first.

JavaScript

which means

JavaScript

to

JavaScript

And clearTimeout to prevent memory leak

I have fixed your sandboxcode:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement