Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

180
Visualizações
Count to numbers above a billion in a few seconds without browser freezing

I am trying to make a counter that counts to big numbers such as 6 billion (like google's random number generator) but the browser freezes.

var counter = document.querySelector("#blahblahblah");
var nb = 0;
var ne = 6_000_000_000;
for (;nb<=ne;nb++) {
counter.innerHTML = nb;
};
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The best thing you can do is use requestAnimationFrame. This represents the fastest rate that you can update the DOM.

Use the callback to update your element text to the number proportional to the time allowed; 3 seconds (a few) from your question title

const runCounter = (num, timeout, el) => {
  let start;

  const step = (ts) => {
    if (!start) {
      start = ts; // record the first iteration timestamp
    }

    const progress = (ts - start) / timeout; // how far into the time span

    if (progress < 1) {
      el.textContent = Math.floor(num * progress);
      requestAnimationFrame(step); // request the next frame
    } else {
      el.textContent = num; // finish
    }
  };

  requestAnimationFrame(step);
};

runCounter(6_000_000_000, 3000, document.getElementById("blahblahblah"));
<output id="blahblahblah">0</output>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda