鼠標(biāo)點(diǎn)開(kāi)始能抽獎(jiǎng),點(diǎn)結(jié)束沒(méi)反映,求大神看一下
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>拖拽效果示例</title>
<style>
div{margin:0 auto;border:1px solid #f00;width:250px;height:130px;}
h1{text-align:center;}
p:nth-of-type(1){float:left;}
p:nth-of-type(2){float:right;}
</style>
<body>
<div>
<h1 id=numarray>開(kāi)始抽獎(jiǎng)啦</h1>
<p id=play>開(kāi)始</p>
<p id=stop>停止</p>
</div>
<script>
var data=['iphone','蘋(píng)果電腦','三星筆記本','50元充值卡','100元現(xiàn)金'];
var numarray=document.getElementById('numarray');
var play=document.getElementById('play');
var stop=document.getElementById('stop');
var timer=null;
// 開(kāi)始抽獎(jiǎng)
play.onclick=playfun;
stop.onclick=stopfun;
document.onkeyup=function(event){
var enter=event.keyCode;
if(enter==13){playfun();}
}
function playfun(){
var timer=setInterval(function(){
var randomm=Math.floor(Math.random()*data.length);
numarray.innerHTML=data[randomm];
},100)
}
function stopfun(){clearInterval(timer);}
</script>
</body>
</html>
2018-07-25
把timer前面var去掉就可以了。
在playfun()函數(shù)里,timer前面加var聲明,這樣timer就成了函數(shù)內(nèi)部變量,在playfun()外部是訪問(wèn)不到的。
2018-06-05