我的 javascript setinterval 循环乘以 2 而不是加 1

My javascript setinterval loop multiplying by two instead of adding 1

我正在尝试构建一个游戏循环,它可以很好地处理两个循环,但是当我尝试添加第三个循环时,它只是乘以二(我的 "fires" var) 而不是添加“1”。你能告诉我哪里有问题吗?

function buyFire(){
    var fireCost = Math.floor(10 * Math.pow(1.1,fires));     //works out the cost of this cursor
    if(cookies >= fireCost){                                   //checks that the player can afford the cursor
        fires = fires + 1;                                   //increases number of cursors
        cookies = cookies - fireCost;                          //removes the cookies spent
        document.getElementById('fires').innerHTML = fires;  //updates the number of cursors for the user
        document.getElementById('cookies').innerHTML = cookies;


    };
    var nextCost = Math.floor(10 * Math.pow(1.1,fires));       //works out the cost of the next cursor
    document.getElementById('fireCost').innerHTML = nextCost;  //updates the cursor cost for the user
};


window.setInterval(function() {
    cookieClick(caves);
}, 900);  

window.setInterval(function() {
    cookieClick(cursors);
}, 1000);

setInterval(function() {
    fireClick(fires);
}, 1200);

完整代码:https://pastebin.com/4rJGWVFG

你给火是 1,然后加上一个数字,在这种情况下,它会自己点火。

var fires = 1;

function fireClick(number){
   fires = fires + number;
   console.log(fires);
};

setInterval(function(){ fireClick(fires); }, 1200);

这就是你想要做的:

var fires = 1;

function fireClick(number){
    fires += number;
    console.log(fires);
};

setInterval(function(){ fireClick(1); }, 1200);

而不是像这样添加:fires = fires + number;
你可以这样写:fires += number;

问题出在 setInterval() 函数上。您有重复的 setIntervals:

var cookies = 0;
var fires = 0;
var cursors = 0;
var caves = 0;

document.getElementById("1").style.visibility = "hidden";
document.getElementById("1.1").style.visibility = "hidden";
document.getElementById("2").style.visibility = "hidden";
document.getElementById("3").style.visibility = "hidden";

function cookieClick(number) {
    cookies = cookies + number;
    document.getElementById("cookies").innerHTML = cookies;
};

function fireClick(number) {
    fires += number;
    document.getElementById("fires").innerHTML = fires;
};

function buyCursor() {
    //works out the cost of this cursor
    var cursorCost = Math.floor(10 * Math.pow(1.1, cursors));
    //checks that the player can afford the cursor
    if (cookies >= cursorCost) {
        //increases number of cursors
        cursors = cursors + 1;
        //removes the cookies spent
        cookies = cookies - cursorCost;
        //updates the number of cursors for the user
        document.getElementById('cursors').innerHTML = cursors;
        //updates the number of cookies for the user
        document.getElementById('cookies').innerHTML = cookies;
        document.getElementById("1").style.visibility = "visible";
    };
    //works out the cost of the next cursor
    var nextCost = Math.floor(10 * Math.pow(1.1, cursors));
    //updates the cursor cost for the user
    document.getElementById('cursorCost').innerHTML = nextCost;
};

function buyCave() {
    //works out the cost of this cursor
    var caveCost = Math.floor(10 * Math.pow(1.1, caves));
    //checks that the player can afford the cursor
    if (cookies >= caveCost) {
        //increases number of cursors
        caves = caves + 1;
        //removes the cookies spent
        cookies = cookies - caveCost;
        //updates the number of cursors for the user
        document.getElementById('caves').innerHTML = caves;
        document.getElementById('cookies').innerHTML = cookies;
        document.getElementById("1.1").style.visibility = "visible";
        //makes update 1 visible
        document.getElementById("2").style.visibility = "visible";
    };
    //works out the cost of the next cursor
    var nextCost = Math.floor(10 * Math.pow(1.1, caves));
    //updates the cursor cost for the user
    document.getElementById('caveCost').innerHTML = nextCost;
};

function fireUp() {
    if (cookies >= 1) {
        document.getElementById("3").style.visibility = "visible";
    };
};

function buyFire() {
    //works out the cost of this cursor
    var fireCost = Math.floor(10 * Math.pow(1.1, fires));
    //checks that the player can afford the cursor
    if (cookies >= fireCost) {
        //increases number of cursors
        fires = fires + 1;
        //removes the cookies spent
        cookies = cookies - fireCost;
        //updates the number of cursors for the user
        document.getElementById('fires').innerHTML = fires;
        document.getElementById('cookies').innerHTML = cookies;
    };
    //works out the cost of the next cursor
    var nextCost = Math.floor(10 * Math.pow(1.1, fires));
    //updates the cursor cost for the user
    document.getElementById('fireCost').innerHTML = nextCost;
};

window.setInterval(function() {
    cookieClick(caves);
}, 900);

window.setInterval(function() {
    cookieClick(cursors);
}, 1000);

window.setInterval(function() {
    fireClick(1);
}, 1200);
<div class=col-xs-3>       
  <span id="2">
    <center>
      Технологии
      <br />
      <button onclick="fireUp(1)">Приручение огня</button>
      <br />
    </center>
  </span>
</div>

<div class=col-xs-6>
  <center>
    <button onclick="cookieClick(1)">Человек</button>
    <br /> Население: <span id="cookies">0</span>
    <br /> <button onclick="buyCursor()">Buy Cursor</button>
    <br /> Cursors: <span id="cursors">0</span>
    <br /> Cursor Cost: <span id="cursorCost">10</span>
    <br />
    <span id="1">
      <button onclick="buyCave()">Найти пещеру</button>
      <br /> Caves: <span id="caves">0</span>
      <br /> Cave Cost: <span id="caveCost">10</span>
    </span>
    <br />
    <span id="1.1">Люди замерзают, необходимы костры</span>
  </center>
</div>

<div class=col-xs-3>
  <span id="3">
    Постройки
    <br /><button onclick="fireClick(1)">Костер</button>
    <br />Костров: <span id="fires">0</span>
    <br /><button onclick="buyFire()">Разводитель костров</button>
    <br />
    <span id="fireCost">10</span>
  </span>
</div>

这可能已经在评论中得到回答,但您在完整来源中指定了您的 fireClick() 如下:

function fireClick(number){
    fires = fires + number;
    document.getElementById("fires").innerHTML = fires;
};

您可能打算将其称为 fireClick(1),而不是 fireClick(fires),returns 会触发 *2。