【发布时间】:2020-06-04 17:19:18
【问题描述】:
如果我点击按钮的速度太快,那么它就不会像没有注册点击一样执行。有什么办法可以让这个简单的代码反应更好?或者这几乎是它的程度? 任何建议都会对我在未来的小型项目中有所帮助。
// function named change_color which contains color array and random selection
function change_Color(){
// creating color variable equal to the color array
var color = ["blue", "red", "green", "yellow"];
// created random variable targeting the color variable saying to choose
// a random color from the array
var random = color[Math.floor(Math.random()*color.length)];
// targets the the div element with id wrapper, telling it to use the
// random variable
document.getElementById("wrapper").style.backgroundColor = random;
}
body {
margin: 0;
}
* {
box-sizing: border-box;
}
#wrapper {
width: 100%;
height: 100vh;
background-color: skyblue;
display: flex;
justify-content: center;
align-items: center;
button {
padding: 10px;
background: orange;
color: white;
border: none;
border-radius: 10px;
font-size: 2rem;
transition: .5s ease;
&:hover {
box-shadow: 0 4px 3px 0px rgba(255, 255, 255, 0.8);
cursor: pointer;
}
&:focus {
outline: 0;
color: skyblue;
}
}
}
<body>
<div id="wrapper">
<button onclick="change_Color()">Click me!</button>
</div>
<script src="JS/style.js"></script>
</body>
【问题讨论】:
-
您还可以连续多次获得完全相同的颜色(几率为 1/4),这使得事件看起来好像没有注册。会是这样吗?
标签: javascript performance button colors reactive