clearTimeout()
?//?JavaScript?Document
var?t1,t2,t3;
//閃爍函數
function?flush()
{
intrvl=0;
var?title=document.getElementById('title');
for(nTimes=0;nTimes<100;nTimes++)
{
intrvl?+=?200;
t1=setTimeout("title.style.background='pink';",intrvl);
intrvl?+=?200;
t2=setTimeout("title.style.background='blue';",intrvl);
intrvl?+=200;
t3=setTimeout("title.style.background='yellow'",intrvl);
??}
}
//實現關閉色彩變化
//抽獎類
var?data=['相機','手機','打印機','謝謝參與','購物券'],
timer=null,
flag=0;//記錄鍵盤的次數
window.onload=function(){
var?plays=document.getElementById('play'),
stops=document.getElementById('stop');
//開始抽獎
plays.onclick=playFun;
stops.onclick=stopFun;
//鍵盤事件
document.onkeyup=function(event){
event=event||window.event;
if(event.keyCode==13){
if(flag==0){
playFun();
flush();
flag=1;
}else{
stopFun();
stopflush();
flag=0;
}
}
}
}
function?playFun(){
var?that=this;
var?title=document.getElementById('title'),
????playsStyle=document.getElementById('play');
clearInterval(timer);
timer=setInterval(function(){
var?randoms=Math.floor(Math.random()*data.length);
title.innerHTML=data[randoms];},80);
playsStyle.style.background="#999";
flush();
}
function?stopFun(){
clearInterval(timer);
var?plays=document.getElementById('play');
plays.style.background='pink';
}
潭州學子-1958?2016-04-06?17:50:37
大神們,請問這里要實現關閉色彩變化,應該如何做?
潭州學子-1958?2016-04-06?17:51:38
我用寫了一個stopFlush(){clearTimeout(t1)...t2..t3;}關閉不了?
2016-04-11
你這邏輯好奇怪呀,按照你這樣寫要關閉色彩變化的話是要停止循環,定義一個變量stop值為false,stopFlush中將stop置為true,循環中判斷stop的值來確認是否跳出循環,不過建議修改一下整個的實現邏輯