bug问题
var data=['Phone5','Ipad','三星笔记本','佳能相机','惠普打印机','谢谢参与','50元充值卡','1000元超市购物券'], timer=null, flag=0; window.onload=function(){ var play=document.getElementById('play'), stop=document.getElementById('stop'); // 开始抽奖 play.onclick=playFun; stop.onclick=stopFun; // 键盘事件 document.onkeyup=function(event){ event = event || window.event; if(event.keyCode==13){ if(flag==0){ playFun(); }else{ stopFun(); } } } } function playFun(){ var title=document.getElementById('title'); var play=document.getElementById('play'); flag = 1; clearInterval(timer); timer=setInterval(function(){ var random=Math.floor(Math.random()*data.length); title.innerHTML=data[random]; },50); play.style.background='#999'; } function stopFun(){ flag = 0; clearInterval(timer); var play=document.getElementById('play'); play.style.background='#036'; }
flag应该在函数里设置,否则会有bug,当第一次点击鼠标,必须两次enter才能停止