JSFiddle - React, Tailwind, and code Playground
by c_kick
HTML
<input type="button" id="test" value="click me!">
JavaScript
// Generates a unique number from a range
// keeps track of generated numbers in a history array
// if all numbers in the range have been returned once, keep outputting random numbers within the range
var interval;
var UniqueRandom = { NumHistory: new Array(), generate: function(maxNum) {
var current = Math.round(Math.random()*(maxNum-1));
if (maxNum > 1 && this.NumHistory.length > 0) {
if (this.NumHistory.length != maxNum) {
while($.inArray(current, this.NumHistory) != -1) { current = Math.round(Math.random()*(maxNum-1)); }
this.NumHistory.push(current);
return current;
} else {
//unique numbers done, continue outputting random numbers
alert('done outputting unique numbers');
clearInterval(interval);
return false;
}
} else {
//first time only
this.NumHistory.push(current);
return current;
}
}
};
$(document).ready(function () {
$('#test').click(function() {
interval = setInterval(function(){
var $random = UniqueRandom.generate(1000);
if($random) $('#test').after($random+", ");
}, 10);
});
});