第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

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


正在回答

8 回答

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


0 回復(fù) 有任何疑惑可以回復(fù)我~
#1

隼丶

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

良辰瑾空人心 提問(wèn)者

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

你把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)作全局變量使用。

0 回復(fù) 有任何疑惑可以回復(fù)我~

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

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

window.onload?=?function(){
?var?title?=?document.getElementById('title'),
??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();
????//?flag?=?1;
???}else?{
????stopFun();
????//?flag?=?0;
???}
??}
?}
}

function?playFun(){
?play.style.backgroundColor?=?'#999';
?clearInterval(timer);
?timer?=?setInterval(function(){
??//?隨機(jī)
??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 回復(fù) 有任何疑惑可以回復(fù)我~
var?data?=?["iphone",?"ipad",?"sony手機(jī)",?"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 回復(fù) 有任何疑惑可以回復(fù)我~

為什么這個(gè)又可以 我第一次發(fā)的那個(gè)就不行

0 回復(fù) 有任何疑惑可以回復(fù)我~

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


0 回復(fù) 有任何疑惑可以回復(fù)我~

能詳細(xì)點(diǎn)說(shuō)那個(gè)的問(wèn)題嗎

0 回復(fù) 有任何疑惑可以回復(fù)我~

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

0 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消

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

我要回答 關(guān)注問(wèn)題
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)