為什么把flag的賦值放在函數(shù)里面不行
<!DOCTYPE html>
<html>
<head>
? ? <meta charset="utf-8">
<title>抽獎(jiǎng)</title>
<style type="text/css">
*{margin: 0;padding: 0;}
#top{width: 400px;height: 50px;margin: 20px auto;color: #f00;font-size: 30px;line-height: 50px;}
#btn{margin: 0 auto;display: block;width: 400px;}
#start,#end{width: 100px;height: 40px;line-height: 40px;display: inline-block;font-size: 25px;background-color: #00f;color: #fff;margin-right: 50px;border-radius: 10px;}
</style>
<script type="text/javascript">
var tid=["謝謝參與","100元超市代金券","50元充值卡","索尼數(shù)碼相機(jī)","三星筆記本","iphone6","ipadMini"];
var flag=0;
var timer=null;
window.onload=function(){
var top=document.getElementById('top');
var start=document.getElementById('start');
var end=document.getElementById('end');
? ? ? ? ? ? start.onclick=startFun;
? ? ? ? ? ? end.onclick=endFun;
? ? ? ? ? ? document.onkeyup=function(event){
? ? ? ? ? ? event= event||window.event;
? ? ? ? ? ? if (event.keyCode==13) {
? ? ? ? ? ? if (flag==0) {
? ? ? ? ? ? startFun();
? ? ? ? ? ? // flag=1;
? ? ? ? ? ? }
? ? ? ? ? ? else{
? ? ? ? ? ? endFun();
? ? ? ? ? ? // flag=0;
? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? function startFun(){
? ? ? ? ? ? clearInterval(timer);
? ? ? ? ? ? timer=setInterval(function(){
? ? ? ? ? ? var xxx=Math.floor(Math.random()*tid.length);
? ? ? ? ? ? top.innerHTML=tid[xxx];
? ? ? ? ? ? },50);
? ? ? ? ? ? ? ? start.style.background="#999";
? ? ? ? ? ? ? ? flag=1;
? ? ? ? ? ? }
? ? ? ? ? ? function endFun(){
? ? ? ? ? ? clearInterval(timer);
? ? ? ? ? ? start.style.background="#00f";
? ? ? ? ? ? flag=0;
? ? ? ? ? ? }
}
</script>
</head>
<body>
<div id="top">開(kāi)始抽獎(jiǎng)啦!</div>
<div id="btn">
<input type="button" id="start" value="開(kāi)始">
<input type="button" id="end" value="停止">
</div>
</body>
</html>
2016-08-03
把flag的賦值放在函數(shù)里面,每執(zhí)行一次函數(shù)就定義flag為0,flag永遠(yuǎn)為0,只能執(zhí)行?startFun()。
2016-10-20
你把flag放在函數(shù)里面的話就是一個(gè)局部變量,當(dāng)函數(shù)執(zhí)行的話,這個(gè)變量會(huì)存在,當(dāng)函數(shù)調(diào)用完成后,你的變量flag就會(huì)消失,當(dāng)下次調(diào)用這個(gè)函數(shù)的時(shí)候又會(huì)重新定義。所以只能放在函數(shù)外面當(dāng)作全局變量使用。
2016-08-23
試了一下是可以的,我的js文件如下:
2016-07-30
2016-07-22
為什么這個(gè)又可以 我第一次發(fā)的那個(gè)就不行
2016-07-22
<!doctype html>
<html>
?<head>
? ?<title>抽獎(jiǎng)</title>
? ?<meta charset="utf-8">
? ?<style type="text/css">
?*{margin:0;
? padding:0;}
.title{width:400px;
? ? ? ?height:70px;
? ? ? ?margin:0 auto;
? ? ? ?padding-top:30px;
? ? ? ?text-align:center;
? ? ? ?font-size:24px;
? ? ? ?font-weight:bold;
? ? ? ?color:#F00;}
.btns{width:190px;
? ? ? height:30px;
? ? ? margin:0 auto;}
.btns span{display:block;
? ? ? ? ? ?float:left;
? ? ? ? ? ?width:80px;
? ? ? ? ? ?height:27px;
? ? ? ? ? ?line-height:27px;
? ? ? ? ? ?background:#036;
? ? ? ? ? ?border:1px solid #eee;
? ? ? ? ? ?border-radius:7px;
? ? ? ? ? ?margin-right:10px;
? ? ? ? ? ?color:#FFF;
? ? ? ? ? ?text-align:center;
? ? ? ? ? ?font-size:14px;
? ? ? ? ? ?font-family:"微軟雅黑";
? ? ? ? ? ?cursor:pointer;}
? ?</style>
? ?<script type="text/javascript" >
? ? ? var data=['Phone5','Ipad','三星筆記本','佳能相機(jī)','惠普打印機(jī)','謝謝參與','50元充值卡','1000元超市購(gòu)物券'],
? ? timer=null,
? ? flag=0;
window.onload=function(){
? ? var play=document.getElementById('play'),
? ? ? ? stop=document.getElementById('stop');
? ? // 開(kāi)始抽獎(jiǎng)
? ? play.onclick=playFun;
? ? stop.onclick=stopFun;
? ?// 鍵盤(pán)事件
? ?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');
clearInterval(timer);
timer=setInterval(function(){
? var random=Math.floor(Math.random()*data.length);
? title.innerHTML=data[random];
},50);
? ? play.style.background='#999';
? flag=1;
}
function stopFun(){
clearInterval(timer);
var play=document.getElementById('play');
play.style.background='#036';
flag=0;
}
? ?</script>
?</head>
?<body>
? ? <div id="title" class="title">開(kāi)始抽獎(jiǎng)啦!</div>
? ? <div class="btns">
? ? ? ?<span id="play">開(kāi) 始</span>
? ? ? ?<span id="stop">停 止</span>
? ? </div>
?</body>
</html>
2016-07-22
能詳細(xì)點(diǎn)說(shuō)那個(gè)的問(wèn)題嗎
2016-07-21
全局變量與局部變量http://blog.csdn.net/zyz511919766/article/details/7276089