bug問題
var?data=['Phone5','Ipad','三星筆記本','佳能相機(jī)','惠普打印機(jī)','謝謝參與','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應(yīng)該在函數(shù)里設(shè)置,否則會有bug,當(dāng)?shù)谝淮吸c(diǎn)擊鼠標(biāo),必須兩次enter才能停止
2016-01-02
if(event.keyCode==13){
?????????if(flag==0){
???????????playFun();
?????????}else{
???????????stopFun();
?????????}
判斷flag=0,執(zhí)行函數(shù)playFun(),之后應(yīng)把flag的值定位1,這樣當(dāng)再次按下回車鍵時,flag=1,執(zhí)行函數(shù)stopFun,此時應(yīng)重下噢你設(shè)置flag=0,這樣再次按下回車鍵時,循環(huán)開始!