Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

221
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda