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

75
Views
Play/Pause doesn't work with useEffect and using setInterval

Here is my code. Whenever I play the start button, it spasms out. Any help would be appreciated! I'm wanting to, whenever I press the button, a timer for 1 second to count down based on the truthiness of isPlaying.

const [onBreak, setOnBreak] = useState(false) 
const [isPlaying, setIsPlaying] = useState(false)

useEffect(() => {
  const timerFunc = setInterval(() => {
    setIsPlaying(!isPlaying);
    if (!onBreak) {
      if (sessionSec === 0 && sessionMin !== 0) {
        setSessionSec(59);
        setSessionMin(sessionMin - 1);
      } else if (sessionSec !== 0 && sessionMin !== 0) {
        setSessionSec(sessionSec - 1);
      } else if (sessionSec === 0 && sessionMin === 0) {
        setOnBreak(true);
      }
    } else if (onBreak) {
      if (breakSec === 0 && breakMin !== 0) {
        setBreakSec(59);
        setBreakMin(breakMin - 1);
      } else if (breakSec !== 0 && breakMin !== 0) {
        setBreakSec(breakSec - 1);
      } else if (breakSec === 0 && breakMin === 0) {
        setOnBreak(false);
      }
    } else {
      clearInterval(timerFunc);
    }
  }, 100);
}, [isPlaying]);


<button onClick={() => setIsPlaying(!isPlaying)}>{isPlaying ? 'Pause': 'Play'}</button>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If setIsPlaying(!isPlaying) calls useEffect() then only call setIsPlaying(1isPlaying) once in the onclick. You also need to change the interval duration to 1000 to wait for one second e.g.

const [onBreak, setOnBreak] = useState(false) 
const [isPlaying, setIsPlaying] = useState(false)

useEffect(() => {
  const timerFunc = setInterval(() => {
    if (!onBreak) {
      if (sessionSec === 0 && sessionMin !== 0) {
        setSessionSec(59);
        setSessionMin(sessionMin - 1);
      } else if (sessionSec !== 0 && sessionMin !== 0) {
        setSessionSec(sessionSec - 1);
      } else if (sessionSec === 0 && sessionMin === 0) {
        setOnBreak(true);
      }
    } else if (onBreak) {
      if (breakSec === 0 && breakMin !== 0) {
        setBreakSec(59);
        setBreakMin(breakMin - 1);
      } else if (breakSec !== 0 && breakMin !== 0) {
        setBreakSec(breakSec - 1);
      } else if (breakSec === 0 && breakMin === 0) {
        setOnBreak(false);
      }
    } else {
      clearInterval(timerFunc);
    }
  }, 1000);
}, [isPlaying]);


<button onClick={() => setIsPlaying(!isPlaying)}>{isPlaying ? 'Pause': 'Play'}</button>
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!