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

169
Visualizações
Number is not incrementing by 1 in JavaScript

I have this code where if the opacity is less than or equal to 0, the message number is suppose to go up by 1, but when I run the code, the message number increases by 77 or 152 or 66, etc. Could you help me? My code:

//variables
var x = 0;
var opacity = 0;
var messageNumber = 0;
var talk1 = ["hello", "welcome to idle ball", "potato"];
var lol = 1;


//set opacity to 1
function opacitySet1(speed) {
  document.getElementById("talk").style.opacity = opacity;
  opacity += speed;
}

//set opacity to 0
function opacitySet0(speed) {
  document.getElementById("talk").style.opacity = opacity;
  opacity -= speed;
}


function IntervalManager(flag, animate, time, para1) {
  if (flag) {
    var intervalSet = setTimeout(animate, time, para1)
  }
}


function IntervalManagerII(flag, animate, time, para1) {
  if (flag) {
    var intervalSetII = setTimeout(animate, time, para1)
  }
}

//to delay time
function nothing() {}


function message(startPart) {
  document.getElementById("talk").innerHTML = messageNumber;
  if (opacity >= 0 && lol == 0) {
    setTimeout(nothing, 1);
    IntervalManagerII(true, opacitySet0, 300, 0.005);
    IntervalManager(false)
  }
  if (opacity <= 1 && lol == 1) {
    IntervalManager(true, opacitySet1, 300, 0.005);
    IntervalManagerII(false)
  }
  if (opacity <= 0) {
    lol = 1;
    IntervalManagerII(false);
    messageNumber += 1;
  } //this is the part that is not working
  if (opacity >= 1) {
    lol = 0;
    IntervalManager(false);
  }
};

setInterval(function() {
  message(0)
});

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

0

New answer

After discussing in the comments, it turns out you think JavaScript timers are blocking the execution of the main thread. It does not work this way. Consider the following example (2 is printed almost instantly, and 1 is printed after one second).

> | setTimeout(function(){console.log(1)}, 1000);
  | setTimeout(function(){console.log(2)}, 0);
< | ...
  | 2
  | 1

Also read this article from jQuery's creator.

Since your code is based on a wrong assumption, I think it makes no sense to work on your question any longer.


Old answer

Your setInterval timer is running a lot faster than your setTimeout timers, meaning that it queues a lot of setTimeout timers before starting to increment the opacity. During this time, the message is incremented and printed at interval max speed. After a couple of ms, all setTimeout timers start firing one after the other with almost no delay between them, and interleaving with setInterval timers, which leads to an (almost) unpredictable mess.

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