有點學迷糊了~局部與全局!
局部變量與全局變量,變量play,stop這么頻繁被調(diào)用,為什么不改為全局變量呢?
在函數(shù)外部定義的變量是全局變量,例如timer、flag;
在函數(shù)內(nèi)部定義的變量則是局部變量,僅供這個函數(shù)調(diào)用;
我這么理解對嗎?
那全局函數(shù)和局部函數(shù)呢?它們之間是怎么調(diào)用的呢?
這課的JS源碼在下~
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(); ???????????flag=1; ?????????}else{ ???????????stopFun(); ???????????flag=0; ?????????} ??????} ???} } function?playFun(){ var?title=document.getElementById('title'); var?play=document.getElementById('play'); clearInterval(timer); timer=setInterval(function(){ ???var?random=Math.floor(Math.random()*data.length); ???title.innerHTML=data[random]; },50); ????play.style.background='#999'; } function?stopFun(){ clearInterval(timer); var?play=document.getElementById('play'); play.style.background='#036'; }
2015-10-22
還有就是調(diào)用函數(shù)的時候,什么時候需要加括號,什么時候不需要呢?