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

201
Visualizações
Want to trigger an adding score adding effect in JS

I want to make a trigger an adding score effect when user's score changing.

Here is the code I currently have:

let speed = 200;
let div = document.querySelector('div')
let olddiv = parseFloat(div.textContent)
let interval;
function change(increase){
   let add = increase/speed

   interval = setInterval(function(){
    div.textContent = parseFloat(div.textContent) +add;
    if(div.textContent > olddiv + increase){
        div.textContent = olddiv+ increase
        window.clearInterval(interval)
    }
   },1)
}
change(5000)
<div>1000</div>

The code works fine but I add 25 (increase/speed value) per milliseconds.

I want to make the score adding 1 every times. I tried to use for loop, but for loop will not shows the adding effect.

Also, since the setInterval doesn't accept milliseconds lower than 1, I can't use this code as well (this code will execute 1 ms every time which is too slow.

   let speed = 200;
    let div = document.querySelector('div')
    let olddiv = parseFloat(div.textContent)
    let interval;
    function change(increase){
       let addspeed = speed/increase;
       interval = setInterval(function(){
        div.textContent = parseFloat(div.textContent) +1;
        if(div.textContent > olddiv + increase){
            div.textContent = olddiv+ increase
            window.clearInterval(interval)
        }
       },addspeed)
    }
    change(5000)
<div>1000</div>

Could anyone give me any solution of solving this problem?

Thanks for any responds!

about 4 years ago · Juan Pablo Isaza
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