利用setInterval製作網頁計時器

來源:互聯網
上載者:User

標籤:ack   charset   date()   暫停   function   一個   val   button   round   

 在javascript中利用 setInterval   clearInterval定時 方法去控制顯示時間的增加









1 <!DOCTYPE html> 2 <html> 3 4 <head> 5 <meta charset="UTF-8"> 6 <title>頁面時鐘</title> 7 <script type="text/javascript" src="js/jquery-3.2.1.js"></script> 8 </head> 9 10 <body>11 <!--<input type="button" id="button1"value="OK" />12 <img src="img/00000.jpg" />-->13     <!--添加一個文字框顯示時間,兩個按鈕去控制時間的開始和暫停-->14 <input type="text" size="30" id="one" /><br />15 <input type="button" id="two" value="開始" onclick="fun1()" />16 <input type="button" id="three" value="結束" onclick="fun2()" />17 18 </body>19 20 </html>21 <script type="text/javascript">22 //控制文字框中的內容23 // document.getElementById("one").value="123";24 var id;25 26 function fun1() {27 //確保只開啟一個定時器28 setTime();29 30 if(!id) {31 32 id = window.setInterval(setTime, 1000);33 }34 }35 36 function setTime() {37 var date = new Date();38 document.getElementById("one").value = date.toLocaleString();39 }40 //停止時鐘41 function fun2() {42 43 clearInterval(id);44 id = undefined;45 }46 </script>

 

利用setInterval製作網頁計時器

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.