如图,当点击开始start按钮时开始随机显示,当点击stop按钮时结束
代码如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>点名表</h1>
<button>start</button><button>stop</button>
<script>
var h = document.querySelector('h1');
var btn = document.querySelectorAll('button')
var names = ['同学1','同学2','同学3','同学4','同学5','同学6','同学7','同学8','同学9']
// names[i]
var times;
btn[0].onclick = function(){
clearInterval(times)
times = setInterval(function(){
var i = Math.floor(Math.random()*names.length)
h.innerHTML = names[i]
},1)
}
btn[1].onclick = function(){
clearInterval(times)
}
</script>
</body>
</html>