偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

一篇文章帶你了解JavaScript時間

開發(fā) 前端
本文基于JavaScript基礎(chǔ),介紹了JavaScript 時間事件 setTimeout(),setInterval() 方法,這兩種方法的語法,實際用法和區(qū)別。以及如何去啟動定時器,停止定時器,通過詳細(xì)案例分析。運(yùn)行效果圖的展示。進(jìn)行了詳細(xì)的講解。代碼很簡單,希望能夠幫助你學(xué)習(xí)。

[[400056]]

一、前言

setTimeout(function, milliseconds) 在等待指定的毫秒數(shù)后執(zhí)行函數(shù)。setInterval(function, milliseconds) setTimeout()相同,但會重復(fù)執(zhí)行。

二、時間事件

窗口對象允許在指定的時間間隔執(zhí)行代碼。時間間隔稱為定時事件。

1. setTimeout() 方法

  1. window.setTimeout(function, milliseconds); 

window.setTimeout() 方法可以不用窗口window前綴編寫。

第一個參數(shù)是要執(zhí)行的函數(shù),第二個參數(shù)指示執(zhí)行前的毫秒數(shù)。

  1. <!DOCTYPE html> 
  2. <html lang="en"
  3. <head> 
  4. <meta charset="UTF-8"
  5. <title>項目</title> 
  6. </head> 
  7. <body style="background-color: aqua;\"
  8.  
  9. <p>點(diǎn)擊"試試". 等3秒,這個頁面將提示"Hello".</p> 
  10.  
  11. <button onclick="setTimeout(myFunction, 3000);">試試</button> 
  12.  
  13. <script> 
  14. function myFunction() { 
  15. alert('Hello'); 
  16. </script> 
  17.  
  18.  
  19. </body> 
  20. </html> 

如何停止執(zhí)行?

clearTimeout() 方法停止指定的函數(shù)setTimeout()的執(zhí)行。

語法:

  1. window.clearTimeout(timeoutVariable) 

window.clearTimeout() 方法可以不用窗口window前綴編寫。

clearTimeout() 方法使用setTimeout()返回的變量。

  1. myVar = setTimeout(function, milliseconds); 
  2. clearTimeout(myVar); 

如果該函數(shù)尚未被執(zhí)行,則可以通過調(diào)用 clearTimeout() 方法:

例:

  1. <!DOCTYPE html> 
  2. <html lang="en"
  3. <head> 
  4. <meta charset="UTF-8"
  5. <title>項目</title> 
  6. </head> 
  7. <body style="background-color: aqua;"
  8.  
  9. <p>點(diǎn)擊 "試試". 等3秒。這個頁面將出現(xiàn)一個"Hello".</p> 
  10. <p>單擊“停止”以阻止第一個功能執(zhí)行。</p> 
  11. <p>(您必須在3秒鐘之前單擊“停止”。)</p> 
  12.  
  13. <button onclick="myVar = setTimeout(myFunction, 3000)">試試</button> 
  14.  
  15. <button onclick="clearTimeout(myVar)">停止</button> 
  16.  
  17. <script> 
  18. function myFunction() { 
  19. alert("Hello"); 
  20. </script> 
  21.  
  22. </body> 
  23. </html> 

2. setInterval() 方法

setInterval() 方法在給定的時間間隔內(nèi)重復(fù)給定的函數(shù)。

  1. window.setInterval(function, milliseconds); 

window.setInterval() 方法可以不用窗口window前綴編寫。

第一個參數(shù)是要執(zhí)行的函數(shù)。

第二個參數(shù)指示每次執(zhí)行之間的時間間隔的長度。

例:

執(zhí)行一個稱為“myTimer”的函數(shù),每隔二秒(像一個數(shù)字表)。

  1. <!DOCTYPE html> 
  2. <html lang="en"
  3. <head> 
  4. <meta charset="UTF-8"
  5. <title>項目</title> 
  6. </head> 
  7. <body style="background-color: aqua;"
  8.  
  9. <p>A script on this page starts this clock:</p> 
  10.  
  11. <p id="demo"></p> 
  12.  
  13. <script> 
  14. var myVar = setInterval(myTimer, 1000); 
  15.  
  16. function myTimer() { 
  17. var d = new Date(); 
  18. document.getElementById("demo").innerHTML = d.toLocaleTimeString(); 
  19. </script> 
  20.  
  21.  
  22. </body> 
  23. </html> 

 

(一秒鐘等于1000毫秒)。

如何停止執(zhí)行?

clearInterval() 方法停止指定的函數(shù)setInterval()的執(zhí)行。

  1. window.clearInterval(timerVariable) 

window.clearInterval() 方法可以不用窗口window前綴編寫。

clearInterval() 方法使用從setInterval()返回的變量 。

  1. <!DOCTYPE html> 
  2. <html lang="en"
  3.   <head> 
  4.     <meta charset="UTF-8"
  5.     <title>項目</title> 
  6.   </head> 
  7.   <body style="background-color: aqua;"
  8.  
  9.     <p>A script on this page starts this clock:</p> 
  10.  
  11.     <p id="demo"></p> 
  12.  
  13.     <button onclick="clearInterval(myVar)">停止</button> 
  14.  
  15.     <script> 
  16.       var myVar = setInterval(myTimer, 1000); 
  17.  
  18.       function myTimer() { 
  19.         var d = new Date(); 
  20.         document.getElementById("demo").innerHTML = d.toLocaleTimeString(); 
  21.       } 
  22. </script> 
  23.  
  24.  
  25.   </body> 
  26. </html> 

代碼解析:

運(yùn)行效果:

三、總結(jié)

本文基于JavaScript基礎(chǔ),介紹了JavaScript 時間事件 setTimeout(),setInterval() 方法,這兩種方法的語法,實際用法和區(qū)別。以及如何去啟動定時器,停止定時器,通過詳細(xì)案例分析。運(yùn)行效果圖的展示。進(jìn)行了詳細(xì)的講解。代碼很簡單,希望能夠幫助你學(xué)習(xí)。

希望大家可以根據(jù)文章的內(nèi)容,積極嘗試,有時候看到別人實現(xiàn)起來很簡單,但是到自己動手實現(xiàn)的時候,總會有各種各樣的問題,切勿眼高手低,勤動手,才可以理解的更加深刻。

使用JavaScript 語言,方便大家更好理解,希望對大家的學(xué)習(xí)有幫助。

本文轉(zhuǎn)載自微信公眾號「前端進(jìn)階學(xué)習(xí)交流」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請聯(lián)系前端進(jìn)階學(xué)習(xí)交流公眾號。

 

責(zé)任編輯:姜華 來源: 前端進(jìn)階學(xué)習(xí)交流
相關(guān)推薦

2024-04-19 14:23:52

SwitchJavaScript開發(fā)

2023-09-06 14:57:46

JavaScript編程語言

2023-07-30 15:18:54

JavaScript屬性

2024-01-30 13:47:45

2021-03-05 18:04:15

JavaScript循環(huán)代碼

2021-03-09 14:04:01

JavaScriptCookie數(shù)據(jù)

2021-01-26 23:46:32

JavaScript數(shù)據(jù)結(jié)構(gòu)前端

2021-06-24 09:05:08

JavaScript日期前端

2021-06-04 09:56:01

JavaScript 前端switch

2021-01-29 18:41:16

JavaScript函數(shù)語法

2021-02-02 18:39:05

JavaScript

2020-11-10 10:48:10

JavaScript屬性對象

2020-12-23 08:12:08

javascriptSVG腳本SVG元素

2021-07-02 10:00:50

JavaScriptObject 函數(shù)

2024-05-17 16:22:25

JavaScript

2024-08-16 15:44:53

JavaScriptWhile循環(huán)

2023-06-09 15:25:39

JavaScript窗口屏幕

2023-07-06 14:40:38

2021-05-25 10:15:20

JavaScript 前端作用域

2023-06-15 10:11:08

JavaScript函數(shù)表達(dá)式
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號