Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

132
Views
React interval timer is not exiting when countdown gets to zero

probably a simple answer to my issue, but it is one that I cannot figure out. My countdown keeps on going past zero and into the negatives, I would like it to end when it hits zero. What am I doing wrong?

Thanks!

    const [staticCountdown, setStaticCountdown] = useState(15);
    const [countdown, setCountdown] = useState(15);


    const setCount = (count) => {
        if (!props.timerActive) {
            setStaticCountdown(count);
            setCountdown(count);
        }
    };

    useEffect(() => {
        let interval = null;
        if (props.timerActive) {
            interval = setInterval(() => {
                setCountdown(countdown => countdown - 1);
            }, 1000);
        } else if (!props.timerActive && countdown === 0) {
            clearInterval(interval);
        }
        return () => clearInterval(interval);
    }, [props.timerActive, countdown]);

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You should change

else if (!props.timerActive && countdown === 0)

into

(if (!props.timerActive || countdown === 0)

here is an example based on you code:

const {
  useEffect,
  useState
} = React;
function App(){
 const [timerActive, setTimerActive] = useState(true);
  const handleTimerActive = () => {
    setTimerActive((prev) => !prev);
  };

  return (
    <div className="App">
      <Timer timerActive={timerActive} />
      <button onClick={handleTimerActive}>
        {timerActive ? "Stop" : "Run"}
      </button>
    </div>
  );
}
function Timer(props) {
  const [countdown, setCountdown] = useState(15);
  useEffect(() => {
    let interval = null;
    if (props.timerActive) {
      interval = setInterval(() => {
        setCountdown((countdown) => countdown - 1);
      }, 1000);
    }
    if (!props.timerActive || countdown === 0) {
      clearInterval(interval);
    }
    return () => clearInterval(interval);
  }, [props.timerActive, countdown]);
  const handleReset = () => {
    setCountdown(15);
  };
  return (
    <div>
      <h1>{countdown}</h1>
      <button onClick={handleReset}>Reset</button>
    </div>
  );
}

ReactDOM.render( <App/> ,
  document.getElementById("root")
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script>
<div id="root"></div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!