欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

javascript setTimeout和setInterval計(jì)時(shí)的區(qū)別詳解

 更新時(shí)間:2013年06月21日 11:05:55   作者:  
window對(duì)象有兩個(gè)主要的定時(shí)方法,分別是setTimeout 和 setInteval 他們的語法基本上相同,但是完成的功能取有區(qū)別。

setTimeout方法是定時(shí)程序,也就是在什么時(shí)間以后干什么。干完了就拉倒。
setInterval方法則是表示間隔一定時(shí)間反復(fù)執(zhí)行某操作。
如果用setTimeout實(shí)現(xiàn)setInerval的功能,就需要在執(zhí)行的程序中再定時(shí)調(diào)用自己才行。如果要清除計(jì)數(shù)器需要 根據(jù)使用的方法不同,調(diào)用不同的清除方法:
例如:(1):

復(fù)制代碼 代碼如下:

t=setTimeout('northsnow()',1000);
clearTimeout(t);
(2):
t=setInterval('northsnow()',1000);
clearInteval(t);
setTimeout()

語法
復(fù)制代碼 代碼如下:

var t=setTimeout("javascript語句",毫秒);

第一個(gè)參數(shù)是含有 JavaScript 語句的字符串。這個(gè)語句可能諸如 "alert('5 seconds!')",或者對(duì)函數(shù)的調(diào)用,諸如 alertMsg()"。
第二個(gè)參數(shù)指示從當(dāng)前起多少毫秒后執(zhí)行第一個(gè)參數(shù)。
提示:1000 毫秒等于一秒。

實(shí)例
當(dāng)下面這個(gè)例子中的按鈕被點(diǎn)擊時(shí),一個(gè)提示框會(huì)在5秒中后彈出。

復(fù)制代碼 代碼如下:

<html>
  <head>
<script type="text/javascript">
function timedMsg() {
 var t=setTimeout("alert('5 seconds!')",5000);
 }
</script>
  </head>
  <body>
    <form>
      <input type="button" value="運(yùn)行計(jì)時(shí)!" onClick="timedMsg()">
    </form>
  </body>
</html>
 

setInterval()
setInterval() 方法可按照指定的周期(以毫秒計(jì))來調(diào)用函數(shù)或計(jì)算表達(dá)式。
setInterval() 方法會(huì)不停地調(diào)用函數(shù),直到 clearInterval() 被調(diào)用或窗口被關(guān)閉。由 setInterval() 返回的 ID 值可用作 clearInterval() 方法的參數(shù)。
語法
setInterval(code,millisec[,"lang"])
實(shí)例
復(fù)制代碼 代碼如下:

<html>
<head>
  <meta charset="utf-8"/>
  <title>setInterval實(shí)例 - 新銳工作室</title>
</head>
<body>
<script language="javascript">
  function endo(){
    alert("你好");
  }
  window.setInterval('endo()',5000);
</script>
</form>
<p> (c) Endige.net </p>
</body>
</html>

傳參方法
無論是window.setTimeout還是window.setInterval,在使用函數(shù)名作為調(diào)用句柄時(shí)都不能帶參數(shù), 而在 許多場(chǎng)合必須要帶參數(shù),這就需要想方法解決。例如對(duì)于函數(shù)hello(_name),它用于針對(duì)用戶名顯示歡
迎信息:
復(fù)制代碼 代碼如下:

var userName="jack";
//根據(jù)用戶名顯示歡迎信息
function hello(_name){
  alert("hello,"+_name);
}

這時(shí),如果企圖使用以下語句來使hello函數(shù)延遲3秒執(zhí)行是不可行的:
window.setTimeout(hello(userName),3000);
這將使hello函數(shù)立即執(zhí)行,并將返回值作為調(diào)用句柄傳遞給setTimeout函數(shù),其結(jié)果并不是程序需要的。 而使用字符串形式可以達(dá)到想要的結(jié)果:
這里的字符串是一段JavaScript代碼,其中的userName表示的是變量。 但這種寫法不夠直觀,而且有些場(chǎng)合必須使用函數(shù)名,下面用一個(gè)小技巧來實(shí)現(xiàn)帶參數(shù)函數(shù)的調(diào)用:
復(fù)制代碼 代碼如下:

<script language="JavaScript" type="text/javascript">
<!--
var userName="jack";
//根據(jù)用戶名顯示歡迎信息
function hello(_name){
  alert("hello,"+_name);
}
//創(chuàng)建一個(gè)函數(shù),用于返回一個(gè)無參數(shù)函數(shù)
function _hello(_name){
  return function(){
    hello(_name);
  }
}
window.setTimeout(_hello(userName),3000);
//-->
</script>

這里定義了一個(gè)函數(shù)_hello,用于接收一個(gè)參數(shù),并返回一個(gè)不帶參數(shù)的函數(shù),
在這個(gè)函數(shù)內(nèi)部使用了外部函數(shù)的參數(shù),從而對(duì)其調(diào)用,不需要使用參數(shù)。在 window.setTimeout函數(shù)中,使用_hello(userName)來返回一個(gè)不帶參數(shù)的
函數(shù)句柄,從而實(shí)現(xiàn)了參數(shù)傳遞的功能。
A.當(dāng)要執(zhí)行的方法中不需要參數(shù)時(shí)
復(fù)制代碼 代碼如下:

<script type=”text/javascript”>
//循環(huán)執(zhí)行,每隔3秒鐘執(zhí)行一次showalert()
window.setInterval(showalert, 3000);
function showalert() {
  alert(“你好”);
}
//定時(shí)執(zhí)行,5秒后執(zhí)行show()
window.setTimeout(show,5000);
function show() {
  alert(“Hello”);
}
</script>

B.當(dāng)要執(zhí)行的方法中需要參數(shù)時(shí)
復(fù)制代碼 代碼如下:

<script type=”text/javascript”>
//循環(huán)執(zhí)行,每隔3秒鐘執(zhí)行一次 showalert()
window.setInterval(function(){
  showalert(“你好!”);
}, 3000);
function showalert(mess) {
  alert(mess);
}
//定時(shí)執(zhí)行,5秒后執(zhí)行showalert()
window.setTimeout(function(){
  showalert(“Hello”);
},5000);
</script>

相關(guān)文章

最新評(píng)論