console
window.onload = function () {
var eleP = document.getElementsByTagName('div')[0].getElementsByTagName('p')[0],
eleStart = document.getElementsByTagName('input')[0],
eleStop = document.getElementsByTagName('input')[1],
arr = ["啤酒一瓶", "亲下左边的人", "亲下右边的人", "啤酒一杯", "讲个笑话", "俯卧撑5个", "表演个节目!"],
cou,
t;
function blurAll () {
eleStart.blur();
eleStop .blur();
}
function addEvent(elem, type, fun) {
if (elem.addEventListener) {
elem.addEventListener(type, fun);
} else if (elem.attachEvent) {
elem.attachEvent("on" + type, fun);
} else {
elem["on" + type] = fun;
}
}
function startC () {
t = setInterval(produceRandom,50);
eleStart.disabled = "disabled";
eleStart.style.backgroundColor = "#aaa";
blurAll();
}
function produceRandom () {
do {
cou = Math.floor(Math.random()*arr.length);
} while (eleP.innerHTML == arr[cou]);
eleP.innerHTML = arr[cou]
}
function stopC () {
eleStart.disabled = false;
eleStart.style.backgroundColor = "#119";
clearInterval(t);
blurAll();
}
function press (e) {
e = e || e.event;
if (e.keyCode == 13) {
if (eleStart.disabled) {
stopC();
} else {
startC();
}
}
}
addEvent(document, "keyup", press);
addEvent(eleStart, "click", startC);
addEvent(eleStop, "click", stopC);
}
<body>
<p>我又无聊了,怎么办呢?</p>
<div>
<p>今天别再喝多了,小艾同学!</p>
<input type="button" name="start" value="开始">
<input type="button" name="sotp" value="停止">
</div>
</body>
html, body, div, p {margin: 0; padding: 0; }
div {width: 400px; margin: 0 auto; text-align: center; font-size: 30px; }
div p {display: block; margin: 40px 0; }
input {font-size: 25px; background-color: #119; color: #fff; border-radius: 10px; width: 120px; height: 50px; cursor: pointer; }