javascript setTimeout和setInterval計(jì)時(shí)的區(qū)別詳解
setTimeout方法是定時(shí)程序,也就是在什么時(shí)間以后干什么。干完了就拉倒。
setInterval方法則是表示間隔一定時(shí)間反復(fù)執(zhí)行某操作。
如果用setTimeout實(shí)現(xiàn)setInerval的功能,就需要在執(zhí)行的程序中再定時(shí)調(diào)用自己才行。如果要清除計(jì)數(shù)器需要 根據(jù)使用的方法不同,調(diào)用不同的清除方法:
例如:(1):
t=setTimeout('northsnow()',1000);
clearTimeout(t);
(2):
t=setInterval('northsnow()',1000);
clearInteval(t);
setTimeout()
語法
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秒中后彈出。
<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í)例
<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ì)用戶名顯示歡
迎信息:
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)用:
<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í)
<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í)
<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>
- setTimeout和setInterval的區(qū)別你真的了解嗎?
- javascript setTimeout和setInterval 的區(qū)別
- setInterval()和setTimeout()的用法和區(qū)別示例介紹
- setTimeout()與setInterval()方法區(qū)別介紹
- setInterval和setTimeout停止的方法
- Js setInterval與setTimeout(定時(shí)執(zhí)行與循環(huán)執(zhí)行)的代碼(可以傳入?yún)?shù))
- JavaScript SetInterval與setTimeout使用方法詳解
- Js中setTimeout()和setInterval() 何時(shí)被調(diào)用執(zhí)行的用法
- JavaScript中SetInterval與setTimeout的用法詳解
- setTimeout與setInterval的區(qū)別淺析
相關(guān)文章
微信小程序?qū)崿F(xiàn)多選刪除列表數(shù)據(jù)功能示例
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)多選刪除列表數(shù)據(jù)功能,涉及微信小程序列表數(shù)據(jù)讀取、顯示、刪除等相關(guān)操作技巧,需要的朋友可以參考下2019-01-01JavaScript 函數(shù)調(diào)用規(guī)則
2009-09-09js window對(duì)象屬性和方法相關(guān)資料整理
這篇文章主要介紹了js window對(duì)象屬性和方法相關(guān)資料整理,需要的朋友可以參考下2015-11-11JavaScript Image對(duì)象實(shí)現(xiàn)原理實(shí)例解析
這篇文章主要介紹了JavaScript Image對(duì)象實(shí)現(xiàn)原理實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08