自动生成电话号码:
html
<div id="box"></div>
<button id="btn" onclick="time()">生成</button>
<button id="btn" onclick="stop()">停止</button>
css
#box {
width: 200px;
height: 300px;
background: antiquewhite;
margin: auto;
}
#btn {
width: 50px;
height: 50px;
background: rgba(0, 0, 0, 0.5);
color: white;
border: 0px;
}
js
<script>
var box = document.getElementById('box');
var timer = "";
function telephone() {
var arr = ["13", "16", "18", "17", "15"];
var obj = [];
for (var n = 0; n < 10; n++) {
var phone = Math.floor(Math.random() * arr.length);
var index = arr[phone];
for (let i = 0; i < 9; i++) {
index = index + Math.floor(Math.random() * 10);
}
if (n == 0) {
obj = obj + index;
}
else {
obj = obj + "<br/>" + index;
}
}
box.innerHTML = obj;
}
function time() {
timer = setInterval(telephone, 100);
}
function stop() {
clearInterval(timer);
}
</script>