1. setInterval() 用法_學(xué)習(xí)
//每隔一秒自動(dòng)執(zhí)行方法
var c=0;
function showLogin()
{
alert(c++);
}
//setInterval方法或字符串 ,毫秒,參數(shù)數(shù)組(方法的))
setInterval("showLogin()","1000");
2.setTimeout
setTimeout()在js類中的使用方法
setTimeout (表達(dá)式,延時(shí)時(shí)間)
setTimeout(表達(dá)式,交互時(shí)間)
延時(shí)時(shí)間/交互時(shí)間是以豪秒為單位的(1000ms=1s)setTimeout 在執(zhí)行時(shí),是在載入后延遲指定時(shí)間后,去執(zhí)行一次表達(dá)式,僅執(zhí)行一次setTimeout 在執(zhí)行時(shí),它從載入后,每隔指定的時(shí)間就執(zhí)行一次表達(dá)式
1,基本用法:
執(zhí)行一段代碼:
var i=0;
setTimeout("i+=1;alert(i)",1000);
執(zhí)行一個(gè)函數(shù):
var i=0;
setTimeout(function(){i+=1;alert(i);},1000);
//注意比較上面的兩種方法的不同。
下面再來(lái)一個(gè)執(zhí)行函數(shù)的:
var i=0;
function test(){
i+=1;
alert(i);
}
setTimeout("test()",1000);
也可以這樣:
setTimeout(test,1000);
總結(jié):
setTimeout的原型是這樣的:
iTimerID = window.setTimeout(vCode, iMilliSeconds [, sLanguage])
setTimeout有兩種形式
setTimeout(code,interval)
setTimeout(func,interval,args)
其中code是一個(gè)字符串
func是一個(gè)函數(shù).
注意"函數(shù)"的意義,是一個(gè)表達(dá)式,而不是一個(gè)語(yǔ)句.
比如你想周期性執(zhí)行一個(gè)函數(shù)
function a(){
//...
}
可寫為
setTimeout("a()",1000)
或
setTimeout(a,1000)
這里注意第二種形式中,是a,不要寫成a(),切記!!!
展開(kāi)來(lái)說(shuō),不管你這里寫的是什么,如果是一個(gè)變量,一定是一個(gè)指向某函數(shù)的變量;如果是個(gè)函數(shù),那它的返回值就 要是個(gè)函數(shù)
2,用setTimeout實(shí)現(xiàn)setInterval的功能(每隔一段時(shí)間自動(dòng)執(zhí)行函數(shù))
思路很簡(jiǎn)單,就是在一個(gè)函數(shù)中調(diào)用不停執(zhí)行自己,有點(diǎn)像遞歸
var i=0;
function xilou(){
i+=1;
if(i>10){alert(i);return;}
setTimeout("xilou()",1000);
//用這個(gè)也可以
//setTimeout(xilou,1000);
}
3,在類中使用setTimeout
終于到正題了,其實(shí)在類中使用大家遇到的問(wèn)題都是關(guān)于this的,只要解決了這個(gè)this的問(wèn)題就萬(wàn)事無(wú)憂了。
呵呵。讓我們來(lái)分析一下:
function xilou(){
this.name="xilou";
this.sex="男";
this.num=0;
}
xilou.prototype.count=function(){
this.num+=1;
alert(this.num);
if(this.num>10){return;}
//下面用四種方法測(cè)試,一個(gè)一個(gè)輪流測(cè)試。
setTimeout("this.count()",1000);//A:當(dāng)下面的x.count()調(diào)用時(shí)會(huì)發(fā)生錯(cuò)誤:對(duì)象不支持此屬性或方法。
setTimeout("count()",1000);//B:錯(cuò)誤顯示:缺少對(duì)象
setTimeout(count,1000);//C:錯(cuò)誤顯示:'count'未定義
//下面是第四種
var self=this;
setTimeout(function(){self.count();},1000);//D:正確
}
var x=new xilou();
x.count();
錯(cuò)誤分析:
A:中的this其實(shí)指是window對(duì)象,并不是指當(dāng)前實(shí)例對(duì)象
B:和C:中的count()和count其實(shí)指的是單獨(dú)的一個(gè)名為count()的函數(shù),但也可以是window.count(),因?yàn)閣indow.count()可以省略為count()
D:將變量self指向當(dāng)前實(shí)例對(duì)象,這樣js解析引擎就不會(huì)混肴this指的是誰(shuí)了。
話說(shuō)回來(lái),雖然我們知道setTimeout("this.count()",1000)中的this指的是window對(duì)象,但還是不明白為什么會(huì)是
window對(duì)象^_^(有點(diǎn)頭暈...)
那我們可以想象一下這個(gè)setTimeout是怎樣被定義的:
setTimeout是window的一個(gè)方法,全稱是這樣的:window.setTimeout()
那應(yīng)該是這樣被定義的:
window.setTimeout=function(vCode, iMilliSeconds [, sLanguage]){
//.....代碼
return timer//返回一個(gè)標(biāo)記符
}
所以當(dāng)向setTimeout()傳入this的時(shí)候,當(dāng)然指的是它所屬的當(dāng)前對(duì)象window了。
簡(jiǎn)單實(shí)例:
<!DOCTYPE html>
<html>
<body>
<h1 id="id1">My Heading 1</h1>
<button type="button" id="click" onClick="change()">
點(diǎn)擊這里!</button>
</body>
<script>
var i=1;
function clickButton(){
document.getElementById("click").click();
i++;
}
setInterval("clickButton()","1000");
//setTimeout("clickButton()",1000);
//setTimeout(clickButton,1000);
function change(){
if(i%2==1)
document.getElementById('id1').style.color='red';
else
document.getElementById('id1').style.color='black';
}
</script>
</html>