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

219
Visualizações
Function being executed twice when it shouldn't be

Im making a cookie clicker like game to practice Javascript. The point of the function addFarm(farmAdd) is to add a certain amount of farms when called. Using console.log, i have found that the setinterval executes twice every 1000ms instead of once every 1000ms leading to the banana count getting +30 added every second instead of +15. I imagine it is because the addFarm function is called twice in the code but the first time it is in an if statement that shouldnt be getting executed since the addFarm1 var is set to true and not false.

I am new to javascript (about a week) and cant figure out why it does this. Any help would be appreciated

function addFarm(farmAdd) { // this is the function
    farmCount = farmCount + farmAdd;
    farmAdd = 0;
    farmIncrement = farmCount * 5;

    farmCountText.textContent = farmCount + ' (+' + farmIncrement + ')';    

    setInterval(function() { // this part is executed twice every 1000ms
        bananaCount = bananaCount + farmIncrement;
        bananaCountText.textContent = bananaCount;
        console.log(farmIncrement);
    }, 1000);
}

function bananaClick() {
    bananaCount += 1;

    if (bananaCount > 10000) {
        bananaClicked.textContent = 'Golden Banana Bunch (+15)';
        banana.src = 'images/banana6.png';
        countByTwo(3);

        if (!addFarm1) {
            addFarm(1);
            addFarm1 = true;
        }
    }

    if (bananaCount > 15000) {
        bananaClicked.textContent = 'Mario Banana (+23)';
        banana.src = 'images/banana7.png';
        countByTwo(4);

        if (!addFarm2) {
            addFarm(2);
            addFarm2 = true;
        }
    }

    bananaCountText.textContent = bananaCount;
}

there is more code but i havent added it so i can make the post shorter but if you need more context then ask please :)

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

0

I imagine when you binded the click event for bananaClick you may be doing it twice.

For instance:

document.addEventListener('click', () => console.log('c'), )
document.addEventListener('click', () => console.log('c'), )

Will print c twice everytime it's clicked.

If there will only be one timer going at a time, you can clear it each time it's called.

let timer;

let a = () => {
   
   clearInterval(timer)
   timer = setInterval(  ()=> {
   console.log('bananas')
  
  }, 2000)
}

a()
a()
a()      // only one timer will be active
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