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才能停止
2016-01-02
if(event.keyCode==13){
?????????if(flag==0){
???????????playFun();
?????????}else{
???????????stopFun();
?????????}
判斷flag=0,執行函數playFun(),之后應把flag的值定位1,這樣當再次按下回車鍵時,flag=1,執行函數stopFun,此時應重下噢你設置flag=0,這樣再次按下回車鍵時,循環開始!