我的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