亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

為什么把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>


正在回答

8 回答

把flag的賦值放在函數里面,每執行一次函數就定義flag為0,flag永遠為0,只能執行?startFun()。


0 回復 有任何疑惑可以回復我~
#1

隼丶

// flag=1;// flag=;去了//
2016-08-03 回復 有任何疑惑可以回復我~
#2

良辰瑾空人心 提問者

非常感謝!
2016-10-20 回復 有任何疑惑可以回復我~

你把flag放在函數里面的話就是一個局部變量,當函數執行的話,這個變量會存在,當函數調用完成后,你的變量flag就會消失,當下次調用這個函數的時候又會重新定義。所以只能放在函數外面當作全局變量使用。

0 回復 有任何疑惑可以回復我~

試了一下是可以的,我的js文件如下:

var?data?=?['iphone5',?'ipad',?'佳能單反',?'迪拜雙人游',?'巴厘島三人游',?'科顏氏套裝',?'NewBalance復古鞋'],
?timer?=?null,
?flag?=?0;

window.onload?=?function(){
?var?title?=?document.getElementById('title'),
??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(){
?play.style.backgroundColor?=?'#999';
?clearInterval(timer);
?timer?=?setInterval(function(){
??//?隨機
??var?random?=?Math.floor(Math.random()?*?data.length);

??title.innerHTML?=?data[random];

?},?50);
?flag?=?1;
}

function?stopFun(){
?clearInterval(timer);
?play.style.backgroundColor?=?'#036';
?flag?=?0;
}


0 回復 有任何疑惑可以回復我~
var?data?=?["iphone",?"ipad",?"sony手機",?"windowsPhone",?"dell電腦"];
var?title?=?document.getElementById("title");
var?startBtn?=?document.getElementById("start");
var?stopBtn?=?document.getElementById("stop");
var?timer?=?null;
var?startFlag?=?false;
function?startFunc()?{
????if?(startFlag)?{
????????return?null;
????}
????clearInterval(timer);
????timer?=?setInterval(function?()?{
????????var?index?=?Math.floor(Math.random()?*?data.length);
????????title.innerHTML?=?data[index];
????},?50);
????startBtn.style.background?=?"gray";
????startBtn.style.cursor?=?"default";
????startFlag?=?true;
}
function?stopFunc()?{
????clearInterval(timer);
????startBtn.style.background?=?"#036";
????startBtn.style.cursor?=?"pointer";
????startFlag?=?false;
}
startBtn.onclick?=?startFunc;
stopBtn.onclick?=?stopFunc;
document.onkeyup?=?function?(e)?{
????console.log(e.keyCode);
????if?(e.keyCode?!==?13)?{
????????return?null;
????}
????!startFlag???startFunc()?:?stopFunc();
}


1 回復 有任何疑惑可以回復我~

為什么這個又可以 我第一次發的那個就不行

0 回復 有任何疑惑可以回復我~

<!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>


0 回復 有任何疑惑可以回復我~

能詳細點說那個的問題嗎

0 回復 有任何疑惑可以回復我~

全局變量與局部變量http://blog.csdn.net/zyz511919766/article/details/7276089

0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

為什么把flag的賦值放在函數里面不行

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號