Q: Javascript issue with mathematical equations Before I begin, I apologize for the possible abundance of code you MAY see in this post. This is the most annoying bug I've ever tried to fix. I've been working for hours, and I still have not found anything.
I have two JavaScript variables, one named addfood and one named clicks. 
The first function below (addBPS) takes the numbers from the variables addfood and clicks and reassigns the sum to the variable clicks once again.
The next chunk of the function just runs an animation of the number counting up in a div. It reads the number in the div, and "animates" it up to the new number in the variable clicks.
The if statement after the function runs the function when the variable addfood is greater than zero. It also runs the function addBPS every second. Lastly, the if statement defines a variable called first and sets it to true.
The next function (which is executed when a button is clicked), reassigns the variable addfood and adds whatever number add had (the variable in the function).
The next line does something similar, and just reassigns the variable clicks and subtracts whatever number cost had (the variable in the function). 
The last if statement, which I believe, is pretty self explanatory. All it does is it runs the function addBPS if the variable addfood is greater than zero. Then it runs the setInterval for the function addBPS only if the variable first does not equal true. (The variable first can also be seen being used on line 19.)
var addfood = 5;
var clicks = 123;

function addBPS() {
    clicks = Number(clicks) + Number(addfood);

    // Countup animation
    $({countNum: $('#current').text()}).animate({countNum: Number(clicks)}, {
      duration: 1000,
      easing:'linear',
      step: function() {
        $('#current').text((Math.floor(this.countNum)));
      },
    });
}
if(Number(addfood) > 0) {
    addBPS();
    setInterval(addBPS, 1000);
    var first = true;
}

function upgradeGame(id, level, cost, add, user) {
    addfood = Number(addfood) + Number(add);
    clicks = Number(clicks) - cost;
    if(Number(addfood) > 0) {
        addBPS();
        if(first != true) {
            setInterval(addBPS, 1000);
        }
    }
}

So now here's where the problem comes in...
The function addBPS is ran every second no matter what. It is supposed to continuously add the value of addfood to clicks every second.
If the value of addfood is at least one, the code works perfectly. However, when the value of addfood is zero, and THEN if I alter the variable and make it one (by using a button), instead of the code continuously adding the value of addfood to clicks, it adds it, and each time, SQUARES the value of addfood.
Example:
Default:
addfood = 0;
var clicks = 0;
Add one to addfood:
addfood = 1;
var clicks (after 1 second) = 1;
Add two to addfood:
addfood = 2;
var clicks (after 1 second) = 4;
Add three to addfood:
addfood = 3;
var clicks (after 1 second) = 9;
What I Expect:
Default:
addfood = 0;
var clicks = 0;
Add one to addfood:
addfood = 1;
var clicks (after 1 second) = 1;
Add two to addfood:
addfood = 2;
var clicks (after 1 second) = 2;
Add three to addfood:
addfood = 3;
var clicks (after 1 second) = 3; 
However, like I said, if the initial value of addfood is one, then the code works fine. When it is zero, and then I change the value of addfood by using a button, the expected value is squared. Why is this happening?

A: Every time upgradeGame() is called, a new Interval will be created. So after the first click, it will be called only once. After the second click it will be called twice, etc. To solve this you need to include
first=true;

So:
function upgradeGame(id, level, cost, add, user) {
addfood = Number(addfood) + Number(add);
clicks = Number(clicks) - cost;
if(Number(addfood) > 0) {
    addBPS();
    if(first != true) {
        setInterval(addBPS, 1000);
        first=true;
    }
}
}

