為什么把flag的賦值放在函數里面不行
<!DOCTYPE html>
<html>
<head>
? ? <meta charset="utf-8">
<title>抽獎</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元充值卡","索尼數碼相機","三星筆記本","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">開始抽獎啦!</div>
<div id="btn">
<input type="button" id="start" value="開始">
<input type="button" id="end" value="停止">
</div>
</body>
</html>
2016-08-03
把flag的賦值放在函數里面,每執行一次函數就定義flag為0,flag永遠為0,只能執行?startFun()。
2016-10-20
你把flag放在函數里面的話就是一個局部變量,當函數執行的話,這個變量會存在,當函數調用完成后,你的變量flag就會消失,當下次調用這個函數的時候又會重新定義。所以只能放在函數外面當作全局變量使用。
2016-08-23
試了一下是可以的,我的js文件如下:
2016-07-30
2016-07-22
為什么這個又可以 我第一次發的那個就不行
2016-07-22
<!doctype html>
<html>
?<head>
? ?<title>抽獎</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','三星筆記本','佳能相機','惠普打印機','謝謝參與','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');
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">開始抽獎啦!</div>
? ? <div class="btns">
? ? ? ?<span id="play">開 始</span>
? ? ? ?<span id="stop">停 止</span>
? ? </div>
?</body>
</html>
2016-07-22
能詳細點說那個的問題嗎
2016-07-21
全局變量與局部變量http://blog.csdn.net/zyz511919766/article/details/7276089