JavaScript學習筆記之定時器,javascript學習筆記

來源:互聯網
上載者:User

JavaScript學習筆記之定時器,javascript學習筆記

定時器1

  用以指定在一段特定的時間後執行某段程式。

  setTimeout():

  格式:[定時器對象名=] setTimeout(“<運算式>”,毫秒)

  功能:執行<運算式>一次。

  例子:

複製代碼 代碼如下:
<!DOCTYPE html>
<html>
  <head>
    <title>timer1.html</title>
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
    <script type="text/javascript">
     function count()
     {
         setTimeout("alert('執行成功!')",7000);
     }
    </script>
  </head>
  <body>
    <input type="button" value="點擊我啊" onclick="count();">
  </body>
</html>

定時器2

  以一定的時間為間隔,不斷地重複執行運算式。

  setInterval():

  格式:[定時器對象名=] setInterval(“<運算式>”,毫秒)

  功能:重複執行<運算式>,直至視窗、架構被關閉或執行clearInterval。

  clearInterval():

  格式:clearInterval(定時器對象名)  

  功能:終止定時器

  例子:

複製代碼 代碼如下:
<!DOCTYPE html>
<html>
  <head>
    <title>timer2.html</title>
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
    <script type="text/javascript">
    var sec = 0;
    var timer = setInterval("count();",1000);//頁面載入的時候即開始計時
     function count()
     {
        document.getElementById("num").innerHTML = sec++;
     }
     function stopCount()
     {
         clearInterval(timer);//停止定時器的運行
     }
    </script>
  </head>
  <body>
    <font color="red" id="num">0</font>
    <input type="button" value="停止" onclick="stopCount();">
  </body>
</html>

以上就是本文的全部內容了,希望大家能夠喜歡

聯繫我們

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