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

93
Views
Contador de alta frecuencia en Reactjs

Necesito escribir un programa como este: el contador se ejecuta en segundo plano contando de 0 a 100 000, luego sigue volviendo a 0 y contando... Hay un botón que manejará el evento: en cada botón, haga clic en el valor de el contador en ese momento se mostrará en la pantalla. Planeé usar setInterval pero mi programa requiere una velocidad muy alta (cuenta hasta 100000 en 1s). ¿Alguien tiene alguna solución?

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

0

Si necesita algo con una frecuencia tan alta, solo mire la hora del sistema y la hora en que comienza a contar, á la

 const counterPosition = ((+new Date() - startTime) * 100000) % 100000;

Aquí hay un ejemplo: una frecuencia de 100,000 puede resultar difícil debido a la inexactitud de new Date() ...

 // TODO: this useRequestAnimationFrame implementation could be buggy; // it is here only to have CounterApp get updated "as fast as it can". function useRequestAnimationFrame() { const [num, setNum] = React.useState(0); const keepUpdatingRef = React.useRef(true); React.useEffect(() => { const requestNext = () => { if(keepUpdatingRef.current) requestAnimationFrame(update); }; const update = () => { setNum(n => n+1); requestNext(); }; requestNext(); return () => keepUpdatingRef.current = false; }, []); return num; } function CounterApp({frequency}) { const frame = useRequestAnimationFrame(); const [startTime] = React.useState(() => +new Date()); const elapsed = (+new Date() - startTime) / 1000.0; const counter = Math.floor((elapsed * frequency) % frequency); return <div>{counter}</div>; } ReactDOM.render(<CounterApp frequency={100} />, document.getElementById("root"));
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.0/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.0/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!