简体   繁体   中英

While-loop crashes browser

My Javscript functions keep crashing the browser now and then. Very few times it crashes, but you have those times, when it does. Using firebug it looks like it is the while loop that is crashing everything. Anyone have any idea?

function generateTeams(pos = 0) {
  // Array of ID's
  var currentTeams = [];
  // 2D array with matches and teamIds
  var matches = [];

  $.each($teamList, function () {
    // Push integer into a new array
    if (this.position >= pos) currentTeams.push(this.id);
  });

  // NumberOfTeams is ALWAYS even numbers, and can be divided by 2
  var numberOfTeams = currentTeams.length;
  var numberOfMatches = numberOfTeams / 2;

  if ((numberOfTeams > 2) && (numberOfTeams % 2 == 0)) {
    var currentCount = numberOfTeams;

    for (var i = 0; i < numberOfMatches; i++) {
      var numOne = Math.floor(Math.random() * currentCount);
      var numTwo = Math.floor(Math.random() * currentCount);

      // Checks if the numbers are the same, or if two spesific teams is against each-other. 
      while ((numOne == numTwo) || (currentTeams[numOne] == 1 && currentTeams[numTwo] == 3) || (currentTeams[numOne] == 3 && currentTeams[numTwo] == 1)) {
        numTwo = Math.floor(Math.random() * currentCount);
      }

      // Creates a match-array with the two team ID's
      matches.push([parseInt(currentTeams[numOne]), parseInt(currentTeams[numTwo])]);

      // Simple way to remove them from the start-array.
      if (numOne > numTwo) {
        currentTeams.splice(numOne, 1);
        currentTeams.splice(numTwo, 1);
      } else {
        currentTeams.splice(numTwo, 1);
        currentTeams.splice(numOne, 1);
      }

      currentCount -= 2;
    } // End for-loop
  } else {
    matches.push([parseInt(currentTeams[0]), parseInt(currentTeams[1])]);
  } // End if

  currentMatches = matches;
} // End generateTeams

It is, first of all, not a good idea to have such an while loop with non deterministic runtime. It can, and statistically will, take a very long time to finish from time to time.

Additionally, there is a condition that makes it impossible to finish: when teams 1 and 3 stay till the end, it can never terminate. As you probably do not have very high numbers of teams, that will happen rather often.

Luckily, the while loop is not necessary at all for the salvation of the given problem: Change your code so that in your for loop, you first select the first team of a match, remove it from currentTeams, then select the second one from the remaining teams. In that way, it's impossible to select the same team twice.

If you really need the condition with the two special teams: remove them from currentTeams first. Then select an opponent for one of them, which makes your first match. Then put the second special team back into the list, and determine the rest of your matches as described before.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM