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

257
Views
Cómo verificar si un número es mayor que otro número dentro de useEffect

Js siendo dificil para mi >:|

 export default function Timer() { const [timer, setTimer] = useState(10000); useEffect(() => { setInterval(() => { if (Number(timer) > Number(6000)) { setTimer((prevCount) => prevCount - 100); } }, 100); }, []); console.log(timer);

¿Por qué este temporizador no deja de contar cuando llega a 6000?

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

0

No necesitará setInterval . Deje que useEffect se ejecute solo cuando cambie el temporizador y esto se hace pasando el timer como argumento.

Este enlace describe cómo optimizar useEffect

 const [timer, setTimer] = useState(10000); function setTimerToNewValue() { setTimer(timer - 100); } useEffect(() => { if (timer > 6000) { setTimerToNewValue(); } }, [timer]); console.log(timer);

En este ejemplo, useEffect se ejecutará tan pronto como cambie el timer .

Aquí hay otra implementación con setInterval

 function setTimerToNewValue() { setTimer((timer) => timer - 100); } useEffect(() => { let z; if (timer > 6000) { z = setInterval(() => { setTimerToNewValue(); }, 1000); } return () => clearInterval(z); }, [timer]);

Demostración aquí

about 4 years ago · Juan Pablo Isaza Report

0

si tiene que usar el temporizador, puede usar este código

 export default function Timer() { const [timer, setTimer] = useState(10000); useEffect(() => { console.log(timer) const interval = setInterval(() => { if (timer>6000) setTimer(timer => timer - 1000); }, 1000); return () => clearInterval(interval); }) }
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!