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

261
Visualizações
Is these a way to add 1 and then after one second than add 2 and than 3 etc

What I am looking for would essentially be a "++" to a "++" command using native java script. The program simply runs an animation for a given number in which the idea of the animatio is that it adds 1 after one second, two after two seconds and keeps going in the same fashion until the animation is stopped.

var counter = 10;
var animationOn = false;
var counterAnimation;
var plusOne;

function updateCounter() {
  //update the counter value 
  var plusOne = counter++;
  for (var i = 1; i = < 100000000;) {

  }

  //show the counter 
  var counterSpan = document.getElementById("counterHolder");
  counterSpan.innerHTML = plusOne;
}

function startCounterAnimation() {
  if (animationOn == false) {
    animationOn == true;
    counterAnimation = setInterval(updateCounter, 1000);
  }
}

function stopCounterAnimation() {
  if (animationOn == true) {
    animationOn == false;
  }
}
<!DOCTYPE html>
<html>

<head>

</head>

<body>
  <button onclick="startCounterAnimation();">
        Start counter animation 
    </button>
  <button onclick="stopCounterAnimation();">
        Stop counter animation
    </button>
  <span id="counterHolder">6931418</span>
</body>

</html>

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

  1. You don't need the for loop.
  2. You should assign to the global plusOne variable, not declare a local variable in the function.
  3. You should add counter++ to it, not assign that directly.
  4. Initialize plusOne from the number already in the output span.
  5. Since your time intervals change between each update, you can't use setInterval(). Use setTimeout() to make a different timeout each time.
  6. Use =, not ==, to assign to the animationOn variable.

var counter;
var animationOn = false;
var counterAnimation;
var plusOne = parseInt(document.getElementById("counterHolder").innerHTML);

function updateCounter() {
  //update the counter value 
  plusOne += counter++;

  //show the counter 
  var counterSpan = document.getElementById("counterHolder");
  counterSpan.innerHTML = plusOne;
  counterAnimation = setTimeout(updateCounter, counter * 1000);
}

function startCounterAnimation() {
  if (!animationOn) {
    animationOn = true;
    counter = 1;
    counterAnimation = setTimeout(updateCounter, 1000 * counter);
  }
}

function stopCounterAnimation() {
  if (animationOn) {
    animationOn = false;
    clearTimeout(counterAnimation);
  }
}
<!DOCTYPE html>
<html>

<head>

</head>

<body>
  <button onclick="startCounterAnimation();">
        Start counter animation 
    </button>
  <button onclick="stopCounterAnimation();">
        Stop counter animation
    </button>
  <span id="counterHolder">6931418</span>
</body>

</html>

about 4 years ago · Juan Pablo Isaza Relatório

0

You can´t define var onePlus in two places, instead define it once and assign a value, var is wide scoped

Also add whatever logic you need in the for loop and put the onePlus ++ and counter as parts of the for loop

As it is now you are expecting to use the counter both outside the for loop and as the for loop middle part, which seems reduntant

Try something like(leaving the variable names as are):

var counter = 1000
for (var plusOne = 1; plusOne < counter; plusOne++) {
    //Your logic in here

    await sleep(1000);
}

function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}
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