Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

218
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!