遇到一个有点奇怪的问题:
单击“开始”再回车“停止”不行,但是如果单击后在别的地方单击一下再回车就能够停止。应该是键盘的事件没有被读取到的原因,但是具体原因就不知道了,下面贴出代码,希望大神不吝指教,谢谢~
js代码:
var arr = ['R','R','R','R','R','R','R','R','R','R','SR','SR','SR','SR','SR','SSR'];
var title = document.getElementById('title'),
timer = null,
flag = 10;
window.onload = function(){
var start = document.getElementById('start'),
stop = document.getElementById('stop');
//开始抽奖
start.onclick = startFunc;
//停止抽奖
stop.onclick = stopFunc;
//键盘事件
document.onkeydown = keyupFunc;
}
function startFunc(){
var title = document.getElementById('title'),
start = document.getElementById('start');
clearInterval(timer);
timer = setInterval(function(){
var random = Math.floor(Math.random()*arr.length);
title.innerHTML = arr[random];
},50)
start.disabled = true;
start.style.backgroundColor = '#ccc';
flag = 11;
}
function stopFunc(){
clearInterval(timer);
start.disabled = false;
start.style.backgroundColor = '#1aa';
flag = 10;
}
function keyupFunc(event){
event = event || window.event;
if(event.keyCode !== 13){
return null;
}else{
flag == 10 ? startFunc() : stopFunc();
}
}