国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 語(yǔ)言 > JavaScript > 正文

setInterval和setTimeout停止的方法

2024-05-06 16:10:44
字體:
供稿:網(wǎng)友
先來了解 setInterval : 
1,HTML DOM setInterval() 方法 
定義和用法 
setInterval() 方法可按照指定的周期(以毫秒計(jì))來調(diào)用函數(shù)或計(jì)算表達(dá)式。 
setInterval() 方法會(huì)不停地調(diào)用函數(shù),直到 clearInterval() 被調(diào)用或窗口被關(guān)閉。由 setInterval() 返回的 ID 值可用作 clearInterval() 方法的參數(shù)。 
語(yǔ)法 
setInterval(code,millisec[,"lang"]) 
參數(shù) 描述 
code 必需。要調(diào)用的函數(shù)或要執(zhí)行的代碼串。 
millisec 必須。周期性執(zhí)行或調(diào)用 code 之間的時(shí)間間隔,以毫秒計(jì)。 
返回值 
一個(gè)可以傳遞給 Window.clearInterval() 從而取消對(duì) code 的周期性執(zhí)行的值。 
2,HTML DOM clearInterval()方法 
定義和用法 
clearInterval() 方法可取消由 setInterval() 設(shè)置的 timeout。 
clearInterval() 方法的參數(shù)必須是由 setInterval() 返回的 ID 值。 
語(yǔ)法 
clearInterval(id_of_setinterval) 
參數(shù) 描述 
id_of_setinterval 由 setInterval() 返回的 ID 值。 
如何停止: 
下面這個(gè)例子將每隔 50 毫秒調(diào)用 clock() 函數(shù)。您也可以使用一個(gè)按鈕來停止這個(gè) clock: 
  1.   
  2. <html>  
  3. <body>  
  4. <input type="text" id="clock" size="35"    />  
  5. <script language=javascript>  
  6. var int=self.setInterval("clock()",50)  
  7. function clock()  
  8. {  
  9. var t=new Date()  
  10. document.getElementById("clock").value=t  
  11. }  
  12. </script>  
  13. </form>  
  14. <button onclick="int=window.clearInterval(int)">  
  15. Stop interval</button>  
  16. </body>  
  17. </html>  

再來了解 setTimeout : 
1,HTML DOM setTimeout() 方法 
定義和用法 
setTimeout() 方法用于在指定的毫秒數(shù)后調(diào)用函數(shù)或計(jì)算表達(dá)式。 
語(yǔ)法 
setTimeout(code,millisec) 
參數(shù) 描述 
code 必需。要調(diào)用的函數(shù)后要執(zhí)行的 JavaScript 代碼串。 
millisec 必需。在執(zhí)行代碼前需等待的毫秒數(shù)。 
提示和注釋 
提示:setTimeout() 只執(zhí)行 code 一次。如果要多次調(diào)用,請(qǐng)使用 setInterval() 或者讓 code 自身再次調(diào)用 setTimeout()。 
實(shí)例,這個(gè)例子,在你點(diǎn)擊按鈕 5 秒鐘后會(huì)彈出一個(gè)提示框: 
  1.   
  2. <html>  
  3. <head>  
  4. <script type="text/javascript">  
  5. function timedMsg()  
  6. {  
  7. var t=setTimeout("alert('5 seconds!')",5000)  
  8. }  
  9. </script>  
  10. </head>  
  11. <body>  
  12. <form>  
  13. <input type="button" value="Display timed alertbox!" onclick="timedMsg()">  
  14. </form>  
  15. <p>Click on the button above. An alert box will be  
  16. displayed after 5 seconds.</p>  
  17. </body>  
  18. </html>  

2,HTML DOM clearTimeout() 方法 
定義和用法clearTimeout() 方法可取消由 setTimeout() 方法設(shè)置的 timeout。語(yǔ)法clearTimeout(id_of_settimeout) 
參數(shù) 描述 
id_of_setinterval 由 setTimeout() 返回的 ID 值。該值標(biāo)識(shí)要取消的延遲執(zhí)行代碼塊。 
實(shí)例下面的例子每秒調(diào)用一次 timedCount() 函數(shù)。您也可以使用一個(gè)按鈕來終止這個(gè)定時(shí)消息:
  1.   
  2. <html>  
  3. <head>  
  4. <script type="text/javascript">  
  5. var c=0  
  6. var t  
  7. function timedCount()  
  8. {  
  9. document.getElementById('txt').value=c  
  10. cc=c+1  
  11. t=setTimeout("timedCount()",1000)  
  12. }  
  13. function stopCount()  
  14. {  
  15. clearTimeout(t)  
  16. }  
  17. </script>  
  18. </head>  
  19. <body>  
  20. <form>  
  21. <input type="button" value="Start count!" onclick="timedCount()">  
  22. <input type="text" id="txt">  
  23. <input type="button" value="Stop count!" onclick="stopCount()">  
  24. </form>  
  25. </body>  
  26. </html>  


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 简阳市| 莆田市| 上杭县| 循化| 乐清市| 景洪市| 连山| 车险| 玉屏| 江都市| 永修县| 静乐县| 敦化市| 沂源县| 安达市| 凤山市| 通许县| 沙湾县| 东阿县| 达孜县| 广南县| 彰化市| 化德县| 上高县| 南阳市| 新巴尔虎右旗| 额尔古纳市| 谷城县| 巍山| 托里县| 平武县| 松滋市| 东明县| 华亭县| 崇阳县| 靖西县| 克什克腾旗| 于田县| 通州区| 建水县| 莱芜市|