溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

JavaScript刪除延時(shí)器的方法是什么

發(fā)布時(shí)間:2021-10-28 09:56:27 來源:億速云 閱讀:220 作者:iii 欄目:web開發(fā)

這篇文章主要講解了“JavaScript刪除延時(shí)器的方法是什么”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“JavaScript刪除延時(shí)器的方法是什么”吧!

JavaScript刪除延時(shí)器的方法:1、使用“clearInterval(id)”語句,可以刪除由setInterval()定義的延時(shí)器;2、使用“clearTimeout(id)”語句,可刪除由setTimeout()定義的延時(shí)器。

JavaScript刪除延時(shí)器的方法是什么

本教程操作環(huán)境:windows7系統(tǒng)、javascript1.8.5版、Dell G3電腦。

JavaScript 延時(shí)器,又稱定時(shí)器,有時(shí)也稱為“計(jì)時(shí)器”,用來在經(jīng)過指定的時(shí)間后執(zhí)行某些任務(wù),類似于我們生活中的鬧鐘。

在 JavaScript 中,我們可以利用延時(shí)器來延遲執(zhí)行某些代碼,或者以固定的時(shí)間間隔重復(fù)執(zhí)行某些代碼。

JavaScript 中提供了兩種方式來設(shè)置定時(shí)器,分別是 setTimeout() 和 setInterval();而對應(yīng)的刪除延時(shí)器的方法也有兩種:

  • clearInterval()    取消由 setInterval() 設(shè)置的 timeout。

  • clearTimeout()    取消由 setTimeout() 方法設(shè)置的 timeout。

clearInterval()

clearInterval() 方法可取消由 setInterval() 設(shè)置的 timeout。

語法:

clearInterval(id)

clearInterval() 方法的參數(shù)必須是由 setInterval() 返回的 ID 值。

示例:

<html>
<body>

<input type="text" id="clock" size="35" />
<script language=javascript>
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>

clearTimeout()

clearTimeout() 方法可取消由 setTimeout() 方法設(shè)置的 timeout。

語法:

clearTimeout(id)

clearInterval() 方法的參數(shù)必須是由 setTimeout() 返回的 ID 值。

示例:

<html>
<head>
<script type="text/javascript">
var c=0
var t
function timedCount()
  {
  document.getElementById('txt').value=c
  c=c+1
  t=setTimeout("timedCount()",1000)
  }
function stopCount()
  {  clearTimeout(t)
  }
</script>
</head>
<body>

<form>
<input type="button" value="Start count!" onClick="timedCount()">
<input type="text" id="txt">
<input type="button" value="Stop count!" onClick="stopCount()">
</form>

</body>
</html>

感謝各位的閱讀,以上就是“JavaScript刪除延時(shí)器的方法是什么”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對JavaScript刪除延時(shí)器的方法是什么這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI