I am trying to add some code where the user clicks a button and the code generates 3 numbers (none repeating, so 245 is OK but 122 and 121 is not) and display them onscreen, one each second. To ensure there are no repeats, I am using an array: var usedNums = [];
. Then, I create the number (digit = Math.random()
) and check if it is already in the array, and if it's not, add it, like this:
if ($.inArray(digit, usedNums) !== -1) {
newNums();
} else {
usedNums.push(digit);
$('#memDigit').html(digit);
}
The first few times, it works, but when I click it for the 10th time, I get the Uncaught RangeError: Maximum call stack size exceeded
error. Help!
Here's the full code:
var usedNums = [];
var digit;
var amount = 3;
function newNums() {
digit = Math.floor(Math.random() * 10);
if ($.inArray(digit, usedNums) !== -1) {
newNums();
} else {
usedNums.push(digit);
$('#memDigit').html(digit);
}
}
function createNums() {
for (var i; i < amount; i++) {
setTimeout(newNums, 1000);
}
}
//$(document).ready(createNums);