Javascript游戏,带有用于崩溃浏览器的循环

Javascript游戏,带有用于崩溃浏览器的循环,javascript,for-loop,Javascript,For Loop,我在我的中世纪主题游戏Teetotum(基于历史版本)中创建了一个迷你游戏,并为它编写了一个脚本,使浏览器无法响应。我假设这是因为我的部分循环无法跳出循环,但我已经一次又一次地检查了我的代码,一切似乎都很好,但有新的眼光来看待它可能会有所帮助 那么,有人能告诉我为什么以下内容会使我的浏览器崩溃吗?更好的是,谁能告诉我如何修复或使其工作 function minigame(game, previous) { var minigameDiv = document.getElementById('m

我在我的中世纪主题游戏Teetotum(基于历史版本)中创建了一个迷你游戏,并为它编写了一个脚本,使浏览器无法响应。我假设这是因为我的部分循环无法跳出循环,但我已经一次又一次地检查了我的代码,一切似乎都很好,但有新的眼光来看待它可能会有所帮助

那么,有人能告诉我为什么以下内容会使我的浏览器崩溃吗?更好的是,谁能告诉我如何修复或使其工作

function minigame(game, previous) {
 var minigameDiv = document.getElementById('minigames');
 show('minigames');
 minigameDiv.innerHTML = "<br>";
  if (game == "teetotum") {
   minigameDiv.innerHTML += "Teetotum is a game of chance, commonly played  with 2 or more players. You spin the top, if it falls on 1, then you take 5 Shrill from the pot. If it falls on 2, no action is taken. If it falls on 3, you add 5 Shrill to the pot. Lastly, if it falls on 4, you win the whole pot.<br><br>";
   minigameDiv.innerHTML += "If you would like to play, set the pot below with the amount you want and how many players you want to play with. <br><br><br>"
   minigameDiv.innerHTML += "<select id='potAmount'> <option selected='selected' value='10'>10 Shrill</option> <option value='25'>25 Shrill</option> <option value='50'>50 Shrill</option></select>Pot Amount<br>"
   minigameDiv.innerHTML += "<select id='playerCount'> <option selected='selected' value='2'>2 Players</option> <option value='3'>3 Players</option> <option value='4'>4 Players</option></select>Player Count<br><br>"      
   minigameDiv.innerHTML += "<a class='button' onclick='javascript:Teetotum(\""+previous+"\");'>Confirm Settings & Begin</a> <br> <a class='button' onclick='javascript:show(\""+previous+"\");'>Go Back to the Tavern</a>";
 }
}


function Teetotum(goBack) {
 playerCount = document.getElementById("playerCount").value;
 totalPlayer = 1;
 potAmount = document.getElementById("potAmount").value;
 var minigameDiv = document.getElementById('minigames');
  if (shrillAmount >= potAmount) {
   shrillAmount = shrillAmount - potAmount;
   checkMoney();
    for (; potAmount > 0;) {
     minigameDiv.innerHTML = "You spin the top...";
     var randomNumber = Math.ceil(Math.random() * 4); 
      setTimeout(function() {
        if (randomNumber == 1) {
           potAmount = potAmount - 5;
           shrillAmount = shrillAmount + 5;
           minigameDiv.innerHTML = "It lands on a 1! You take 5 Shrill from the pot. The pot is "+potAmount+"."
        }
        else if (randomNumber == 2) {
           minigameDiv.innerHTML = "It lands on a 2. Nothing happens. The pot is "+potAmount+"."
        }
        else if (randomNumber == 3 && shrillAmount >= 5) {
           potAmount = potAmount + 5;
           shrillAmount = shrillAmount - 5;
           minigameDiv.innerHTML = "It lands on a 3! You lose 5 Shrill into the pot. The pot is "+potAmount+"."
        }
        else if (randomNumber == 4) {
           shrillAmount = shrillAmount + potAmount;
           potAmount = potAmount - potAmount;
           minigameDiv.innerHTML = "It lands on a 4! You win the whole pot."
        }
        if (potAmount > 0) {
           minigameDiv.innerHTML += "<br><br>The next player(s) will now take their turn."
                 setTimeout(function() {
                 for (playerCount = playerCount; playerCount > totalPlayer && potAmount > 0; playerCount--) {
                    minigameDiv.innerHTML = ('Player '+playerCount+' spins the top...');
                    var randomAINumber = Math.ceil(Math.random() * 4);
                       setTimeout(function() {
                       if (randomAINumber == 1) {
                          potAmount = potAmount - 5;
                          minigameDiv.innerHTML = "It lands on a 1. Player "+playerCount+" takes 5 Shrill from the pot. The pot is "+potAmount+"."
                       }
                       else if (randomAINumber == 2) {
                          minigameDiv.innerHTML = "It lands on a 2. Nothing happens. The pot is "+potAmount+"."
                       }
                       else if (randomAINumber == 3) {
                          potAmount = potAmount + 5;
                          minigameDiv.innerHTML = "It lands on a 3. Player "+playerCount+" puts 5 Shrill into the pot. The pot is "+potAmount+"."
                       }
                       else if (randomAINumber == 4) {
                          shrillAmount = shrillAmount + potAmount;
                          potAmount = potAmount - potAmount;
                          minigameDiv.innerHTML = "It lands on a 4! Player "+playerCount+" wins the pot."
                       }
                       if (potAmount <= 0) {
                          minigameDiv.innerHTML += "<br><br>The game is now finished <br><br><br> <a class='button' onclick='javascript:show(\""+previous+"\");'>Go Back to the Tavern</a>"
                       }
                    }, 2000);
                 }
      }, 3000);
        }
        if (potAmount <= 0) {
           minigameDiv.innerHTML += "<br><br>The game is now finished <br><br><br> <a class='button' onclick='javascript:show(\""+previous+"\");'>Go Back to the Tavern</a>"
        }
      }, 3000);
  }
}
else {
  ui.log('You do not have enough Shrill to do that.');
}
} 
功能小游戏(游戏,上一个){
var minigameDiv=document.getElementById('minigames');
表演(“小游戏”);
minigameDiv.innerHTML=“
”; 如果(游戏==“出牙”){ minigameDiv.innerHTML+=“Teetotum是一种机会游戏,通常由两名或两名以上的玩家玩。你旋转顶部,如果它落在1,则从壶中取出5个尖叫。如果它落在2,则不采取任何行动。如果它落在3,则向壶中加入5个尖叫。最后,如果它落在4,则赢得整个壶。

”; minigameDiv.innerHTML+=“如果您想玩,请在下面设置您想要的数量和您想要玩的玩家数量。


” minigameDiv.innerHTML+=“10只虾25只虾50只虾的数量
” minigameDiv.innerHTML+=“2名玩家3名玩家4名玩家玩家计数

” minigameDiv.innerHTML+=“确认设置并开始
返回酒店”; } } 功能性牙托(goBack){ playerCount=document.getElementById(“playerCount”).value; totalPlayer=1; potAmount=document.getElementById(“potAmount”).value; var minigameDiv=document.getElementById('minigames'); 如果(shrillAmount>=potAmount){ shrillAmount=shrillAmount-potAmount; 支票货币(); 对于(;potAmount>0;){ minigameDiv.innerHTML=“旋转顶部…”; var randomNumber=Math.ceil(Math.random()*4); setTimeout(函数(){ 如果(随机数==1){ potAmount=potAmount-5; shrillAmount=shrillAmount+5; minigameDiv.innerHTML=“它落在一个1!你从罐子里取出5个尖叫声。罐子是“+potamum+” } else if(randomNumber==2){ minigameDiv.innerHTML=“它降落在一个2上。什么也没有发生。罐子是“+potamum+” } 否则如果(随机数==3&&shrillAmount>=5){ potAmount=potAmount+5; shrillAmount=shrillAmount-5; minigameDiv.innerHTML=“它落在一个3!你在罐子里损失了5个尖叫声。罐子是“+potAmount+” } 否则如果(随机数==4){ shrillAmount=shrillAmount+potAmount; potAmount=potAmount-potAmount; minigameDiv.innerHTML=“它落在一个4!你赢了整罐。” } 如果(potAmount>0){ minigameDiv.innerHTML+=“

下一个玩家现在轮到他们了。” setTimeout(函数(){ 对于(playerCount=playerCount;playerCount>totalPlayer&&potAmount>0;playerCount--){ minigameDiv.innerHTML=('Player'+playerCount+'spins the top…'); var randomAINumber=Math.ceil(Math.random()*4); setTimeout(函数(){ 如果(随机数==1){ potAmount=potAmount-5; minigameDiv.innerHTML=“它落在一个1.Player“+playerCount+”上,从罐子中取出5个尖叫声。罐子是“+potAmount+” } else if(随机数==2){ minigameDiv.innerHTML=“它降落在一个2上。什么也没有发生。罐子是“+potamum+” } else if(随机数==3){ potAmount=potAmount+5; minigameDiv.innerHTML=“它落在一个3.Player“+playerCount+”上,将5个尖叫声放入锅中。锅为“+potAmount+” } else if(随机数==4){ shrillAmount=shrillAmount+potAmount; potAmount=potAmount-potAmount; minigameDiv.innerHTML=“它落在一个4!玩家“+playerCount+”上,赢得该壶。” }
如果(potAmount如果您希望游戏继续运行,直到
potAmount
用完,请尝试使用
while()
循环

while(potAmount > 0){
    //your code here
}

如果您希望游戏继续运行,直到您的
potamunt
用完,请尝试使用
while()
循环

while(potAmount > 0){
    //your code here
}

您的代码的问题是,pot从未实际减少。您已将所有减少pot的代码放入多个
setTimeout
调用中,但在您离开循环之前不会执行这些调用,因为pot没有减少,所以您永远不会执行这些调用

如果您的逻辑需要使用
setTimeout
,下面是如何编写循环:

var loopFunction = function() {
        var randomNumber = Math.random() * 4;
        if (randomNumber == 1) {
        }
        ...
        if (potAmount > 0) {
            setTimeout(loopFunction, 3000);
        }
}

这样,下一次迭代只会在上一次迭代结束后开始。

代码的问题是,pot从未实际减少。您已将所有减少pot的代码放入多个
setTimeout
调用中,但它们不会执行,直到您离开循环,因为pot没有减少,所以您永远不会执行该循环d

如果您的逻辑需要使用
setTimeout
,下面是如何编写循环:

var loopFunction = function() {
        var randomNumber = Math.random() * 4;
        if (randomNumber == 1) {
        }
        ...
        if (potAmount > 0) {
            setTimeout(loopFunction, 3000);
        }
}

这样,下一次迭代将仅在上一次迭代结束后开始。

控制台中是否出现错误?是否尝试调试应用程序,浏览器中内置了调试器。控制台中没有错误。我现在将尝试使用浏览器的调试器。控制台中是否出现错误?是否尝试调试应用程序是浏览器中内置的调试器。控制台中没有错误。我将尝试立即使用浏览器的调试器。使用while()循环或循环时,浏览器仍会冻结和崩溃