javascript setInterval setTimeout執行個體(1/2)

來源:互聯網
上載者:User

1,html dom setinterval() 方法
定義和用法
setinterval() 方法可按照指定的周期(以毫秒計)來調用函數或計算運算式。
setinterval() 方法會不停地調用函數,直到 clearinterval() 被調用或視窗被關閉。由 setinterval() 返回的 id 值可用作 clearinterval() 方法的參數。
文法
setinterval(code,millisec[,"lang"])
參數 描述
code 必需。要調用的函數或要執行的代碼串。
millisec 必須。周期性執行或調用 code 之間的時間間隔,以毫秒計。
傳回值
一個可以傳遞給 window.clearinterval() 從而取消對 code 的周期性執行的值。
2,html dom clearinterval()方法
定義和用法
clearinterval() 方法可取消由 setinterval() 設定的 timeout。
clearinterval() 方法的參數必須是由 setinterval() 返回的 id 值。
文法
clearinterval(id_of_setinterval)
參數 描述
id_of_setinterval 由 setinterval() 返回的 id 值。
如何停止:
下面這個例子將每隔 50 毫秒調用 clock() 函數。您也可以使用一個按鈕來停止這個 clock: 
 

<html>
<body>
<input type="text" id="clock" size="35" />
<script language=網頁特效>
var int=self.setinterval("clock()",50)
function clock()
{
var t=new date()
document.getelementbyid("clock").value=t
}
</script>
</form>
<button onclick="int=window.clearinterval(int)">
stop interval</button>
</body>
</html>

  [ctrl+a 全選 注:如需引入外部js需重新整理才能執行]
再來瞭解 settimeout :
1,html dom settimeout() 方法
定義和用法
settimeout() 方法用於在指定的毫秒數後調用函數或計算運算式。
文法
settimeout(code,millisec)
參數 描述
code 必需。要調用的函數後要執行的 javascript 代碼串。
millisec 必需。在執行代碼前需等待的毫秒數。
提示和注釋
提示:settimeout() 只執行 code 一次。如果要多次調用,請使用 setinterval() 或者讓 code 自身再次調用 settimeout()。
執行個體,這個例子,在你點擊按鈕 5 秒鐘後會彈出一個提示框: 
 

<html>
<head>
<script type="text/javascript">
function timedmsg()
{
var t=settimeout("alert('5 seconds!')",5000)
}
</script>
</head>
<body>
<form>
<input type="button" value="display timed alertbox!"
onclick="timedmsg()">
</form>
<p>click on the button above. an alert box will be
displayed after 5 seconds.</p>
</body>
</html>

 

首頁 1 2 末頁

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.