<!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>
<style>
div {
width: 200px;
height: 200px;
border: 1px solid #c93939;
background-color: chartreuse;
}
input {
margin: 8px 0;
}
</style>
</head>
<body>
<input type="button" value="开始" id="btn1">
<input type="button" value="结束" id="btn2">
<div id="dv"></div>
</body>
<script>
var btn1 = document.getElementById("btn1");
var btn2 = document.getElementById("btn2");
var dv = document.getElementById("dv");
function getColor() {
var str = "#";
var arr = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "f"];
for (var i = 0; i < 6; i++) {
var num = parseInt(Math.random() * 16);
str += arr[num];
}
dv.style.backgroundColor=str;
}
btn1.onclick = function () {
timeId=setInterval("getColor()",100);
}
btn2.onclick = function () {
clearInterval(timeId);
timeId=null;
}
</script>
</html>
网友评论