Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

83
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda