默认是随机生成1~10内的十个数
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<input type="text" id="have" placeholder="例外,以逗号分隔">
<br>
<input type="text" id="beg" placeholder="生成范围开始0-100">
<input type="text" id="end" placeholder="生成范围结束0-100">
<button onclick="start();">开始</button>
<div id="content"></div>
<script type="text/javascript">
Date.prototype.Format = function (fmt) { // author: meizz
var o = {
"M+": this.getMonth() + 1, // 月份
"d+": this.getDate(), // 日
"h+": this.getHours(), // 小时
"m+": this.getMinutes(), // 分
"s+": this.getSeconds(), // 秒
"q+": Math.floor((this.getMonth() + 3) / 3), // 季度
"S": this.getMilliseconds() // 毫秒
};
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
}
for (var k in o) {
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
}
return fmt;
}
var count = 0;
var arr = new Array();
function generateR() {
var r = parseInt(Math.random() * 100);
if (!isNaN(r) && r >= beg && r <= end) {
var hasR = false;
for(var index in arr){
if(arr[index] == r){
return generateR();
}
}
return r;
} else {
return generateR();
}
}
var beg, end, have, timmer;
function start() {
have = document.getElementById('have').value;
if(have){
var tmp = have.split(",");
for(var index in tmp){
arr.push(tmp[index]);
}
}
beg = document.getElementById('beg').value;
if (!beg) {
beg = 0;
}
end = document.getElementById('end').value;
if (!end) {
end = 10;
}
console.log('begin' + document.getElementById('beg').value);
timmer = setInterval(function () {
count++;
if (count > 20) {
return;
}
var d = new Date().Format("yyyy-MM-dd hh:mm:ss");
var r;
try {
r = generateR();
} catch (error) {
clearInterval(timmer);
console.log(error);
return;
}
arr.push(r);
console.log(parseInt(r) + " -- " + d);
writeTo('content', r + " -- " + d);
}, 1000);
}
function writeTo(e, v) {
document.getElementById(e).innerHTML += v + '<br>';
}
</script>
</body>
</html>
来源:CSDN
作者:赫双炏
链接:https://blog.csdn.net/heliangb46/article/details/103462059