我的Javscript函數不時崩潰瀏覽器,然後。很少有它崩潰,但你有那些時候,當它的時候。使用螢火蟲它看起來就像是一個讓所有東西崩潰的while循環。任何人有任何想法?While循環崩潰瀏覽器
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
這意味着無論循環內部發生什麼,while循環頂部的條件都保持爲真。您可以添加一些'console.log()'調用來跟蹤涉及的值。 – Pointy