-1

I can't seem to figure out why the farmerTime is not updating when you level up. There is a button that just adds a level to farmingLevel.

    window.setInterval(function() {
            farmerTime = 2500;
            farmerLevel = 3;

            x = farmerTime;
            y = farmerLevel;
            z = x / y;
            farmerTime = z;

            if (farmers >= 1) {
                    a = farmers;
                    b = potatoes;
                    c = a * 1;
                    d = b + c;
                    potatoes = d;
            }
    }, farmerTime);`
Potato
  • 13
  • 1
  • 6
  • 1
    Define `framerTime` before `setInterval `. If you want to change the interval time dynamically, first you have to `clearInterval` then re initiate it. – Shubham Jun 26 '17 at 12:20
  • 3
    Once an interval is set, it will tick at that rate until you clear it. If you want to change the interval, you need to `clearInterval` the current one (hope you saved a reference to it!) and `setInterval` with the new time. – Niet the Dark Absol Jun 26 '17 at 12:21

1 Answers1

0

You need to define farmerTime before you use it. In your case, before the setInterval function. Also, if you want to change the farmerLevel you need to change it somewhere else, not in the setinterval function.

Changing level example:

<button type="button" onclick="setFarmerLevel(farmerLevel + 1);">Change level </button>

And the code for the interval thing:

var farmerTime  = 2500;
var farmerLevel = 1;

var setFarmerLevel = function (level) {
    farmerLevel = !level ? 1 : level;
    farmerTime  = farmerTime / farmerLevel;
    clearInterval(farmerInterval);
    farmerInterval = window.setInterval(run, farmerTime);
};

var run = function () {
    if (farmers >= 1) {
        a = farmers;
        b = potatoes;
        c = a * 1;
        d = b + c;
        potatoes = d;
    }
};

var farmerInterval = window.setInterval(run, farmerTime);

UPDATE

I forgot setInterval's function time cannot be change in runtime, so the code is updated now.

Dawid Zbiński
  • 5,521
  • 8
  • 43
  • 70